PHP curl 類比POST 上傳檔案

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   sp   for   

<?php    /**     * Email [email protected]     * author jackluo     * 2014.11.21
*
*/ //* function curl_post($url, $data, $header = array()){ if(function_exists(‘curl_init‘)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); if(is_array($header) && !empty($header)){ $set_head = array(); foreach ($header as $k=>$v){ $set_head[] = "$k:$v"; } curl_setopt($ch, CURLOPT_HTTPHEADER, $set_head); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 1);// 1s to timeout. $response = curl_exec($ch); if(curl_errno($ch)){ //error return curl_error($ch); } $reslut = curl_getinfo($ch); print_r($reslut); curl_close($ch); $info = array(); if($response){ $info = json_decode($response, true); } return $info; } else { throw new Exception(‘Do not support CURL function.‘); } } //*/ // function api_notice_increment($url, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1);// $data = http_build_query($data); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //curl_file_create // $result = curl_exec($ch); $lst[‘rst‘] = curl_exec($ch); $lst[‘info‘] = curl_getinfo($ch); curl_close($ch); return $lst; // return $result; } /** * curl檔案上傳 * @var struing $r_file 上傳檔案的路勁和檔案名稱 * */ /* function upload_file($url,$r_file) { $file = array("fax_file"=>‘@‘.$r_file,‘type‘=>‘image/jpeg‘);//檔案路徑,前面要加@,表明是檔案上傳. $curl = curl_init(); curl_setopt($curl, CURLOPT_URL,$url); curl_setopt($curl,CURLOPT_POST,1); curl_setopt($curl,CURLOPT_POSTFIELDS,$file); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HEADER, 0); $result = curl_exec($curl); //$result 擷取頁面資訊 curl_close($curl); echo $result ; //輸出 頁面結果 }*/ function upload_file($url,$filename,$path,$type){ $data = array( ‘pic‘=>‘@‘.realpath($path).";type=".$type.";filename=".$filename ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // curl_getinfo($ch); $return_data = curl_exec($ch); curl_close($ch); echo $return_data; } if ($_POST) { $url = ‘http://platform.com/upload/image‘; // $path = $_SERVER[‘DOCUMENT_ROOT‘];/* print_r($_FILES); exit;*/ //$filename = $path."/232.jpg"; //upload tmp $tmpname = $_FILES[‘fname‘][‘name‘]; $tmpfile = $_FILES[‘fname‘][‘tmp_name‘]; $tmpType = $_FILES[‘fname‘][‘type‘];// echo $tmpType; upload_file($url,$tmpname,$tmpfile,$tmpType); /* $data = array( ‘path‘=>"@$path/232.jpg", ‘name‘=>‘h‘ ); */ //‘pic‘=>‘@/tmp/tmp.jpg‘, ‘filename‘=>‘tmp‘ //$data = array(‘pic‘=>"@$filename", ‘filename‘=>‘tmp‘);/* $data = array( ‘uid‘ => 10086, ‘pic‘ => ‘@$tmpfile‘.‘;type=‘.$tmpType ); $info = api_notice_increment($url, $data);*/ //$info = curl_post($url, $data); //$info = api_notice_increment($url, $data); //upload_file($url,$tmpfile); //print_r($info); exit;/* $file = ‘H:\www\test\psuCARGLSPA-pola.jpg‘; //要上傳的檔案 $src = upload_curl_pic($file); echo $src;*/ } ?><form action="http://localhost/upload.php" enctype="multipart/form-data" method="post"> <p>UpLoad: <input type="text" name="fname" /></p> <p>UpLoad: <input type="file" name="fname" /></p> <input type="submit" value="Submit" /></form>

 

PHP curl 類比POST 上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.