php怎麼使用curl傳輸檔案流

來源:互聯網
上載者:User
public function postFile(){   $name = 'filename';   $path = './Resource/temp_pdf/';   $ext = '.pdf';
   if (is_file($path . $name . $ext) && filesize($path . $name . $ext) != 0) {      $url = "http://test.api.com/index.php";      $post_data = array(         "foo" => "bar",         //@代表此欄位屬於檔案,接收方只需用$_FILES便可接收檔案         "upload" => '@' . $path . $name . $ext,      );      $res = httpRequest($url,$post_data);      var_dump($res);      //TODO::擷取返回資料的動作   }}
 
 
 
 
/** * 請求遠程地址 * * @param string $url 請求url * @param mixed $postFields 請求的資料 * @param string $referer 來源網址 * @param integer $timeOut 請求逾時時間 * @param array $header 頭部檔案 * @return mixed 錯誤返回false,正確返回擷取的字串 * @author fengxu */function httpRequest($url, $postFields = null, $referer = null, $timeOut = 300, $header = null){    if (empty($url) || !preg_match("#https?://[\w@\#$%*&=+-?;:,./]+#i", $url)) {        return false;    }    $isPost = empty($postFields) ? false : true;    $ch = curl_init();    if (is_null($header)) {        $header = array(            'Pragma' => 'no-cache',            'Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,q=0.5',            'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36',        );    }    $headers = array();    foreach ($header as $k => $v) {        $headers[] = $k . ': ' . $v;    }    curl_setopt($ch, CURLOPT_URL, $url);    if ($isPost) {        //$postFields = http_build_query($postFields);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);    }    curl_setopt($ch, CURLOPT_TIMEOUT, $timeOut);    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_HEADER, 0);    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    $response = curl_exec($ch);    if ($response === false) {        throw new Exception(curl_error($ch), '500');    }    return $response;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.