php利用curl上傳圖片
阿新 • • 發佈:2022-04-09
public function curlUpload($url = '', $postData = '') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $cfile = curl_file_create($postData['tmp_name'],$postData['type'],$postData['name']); // 設定 POST 資料 $data = array('file' => $cfile); // echo "<pre>";print_r($data);die; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設定cURL允許執行的最長秒數 //https請求 不驗證證書和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch); // var_dump($data); curl_close($ch); return json_decode($data, 1); }
//呼叫例項
$file=$_POST['file']; $url='http://www.baidu.com'; $result=$this->curlUpload($url,$file); echo "<pre>"; print_r( $result);