PHP使用CURL類比POST/GET/PUT/DELETE方式提交資料____PHP

來源:互聯網
上載者:User
  最近因為工作需要,調用網盤介面來上傳檔案,我用了CURL庫, 當然在用CURL庫之前必須要在php中啟用 cURL 設定

  可以通過使用php_info()函數來得到cURL資訊,如果看不到cURL資訊的話,那麼需要設定PHP並開啟這個庫。在Windows平台下,需要改一改php.ini檔案的設定,找到    php_curl.dll,並取消前面的分號注釋就行了。

   一般的檔案上傳是通過html表單進行的,通過CURL可以不經過瀏覽器,直接在伺服器端類比進行表單提交,完成POST資料、檔案上傳等功能。

   我們是可以通過其他辦法擷取網頁內容。大多數時候,我因為想偷懶,都直接用簡單的PHP函數,如下:

  $content = file_get_contents("http://www.doucube.com");
  // or
  $lines = file("http://www.doucube.com");
  // or
  readfile(http://www.doucube.com);

   不過,這種做法缺乏靈活性和有效錯誤處理。而且,你也不能用它完成一些高難度任務——比如處理coockies、驗證、表單提交、檔案上傳等等。所以選擇curl庫。

範例程式碼:

<?php
    $url = 'https://www.google.com';
    $method = 'POST';
     
    //headers and data (this is API dependent, some uses XML):
    //即在介面調用時才用headers 和$data
    $headers = array(
    'Accept: application/json',
    'Content-Type: application/json',
    );
    $data = json_encode(array(
    'firstName'=> 'John',
    'lastName'=> 'Doe'

    ));     // 啟動一個CURL會話
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url); // 要訪問的地址
    curl_setopt($handle,CURLOPT_HEADER,1); // 是否顯示返回的Header地區內容
    curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); //佈建要求頭
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); // 擷取的資訊以檔案流的形式返回
    curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false); // 從認證中檢查SSL密碼編譯演算法是否存在
    curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); // 對認證認證來源的檢查
     
    switch($method) {
    case 'GET':
    break;
    case 'POST':
    curl_setopt($handle, CURLOPT_POST, true);
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data); //佈建要求體,提交資料包
    break;
    case 'PUT':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($handle, CURLOPT_POSTFIELDS, $data); //佈建要求體,提交資料包
    break;
    case 'DELETE':
    curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
    break;
    }
     
    $response = curl_exec($handle); // 執行操作
    $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); // 擷取返回的狀態代碼
    curl_close ($handle); // 關閉CURL會話
    if('200'==$code){
      echo "ok";
    }
    

    下面還有一個樣本,有興趣可以看看:

用curl上傳檔案的話很方便,什麼header,post串都不用產生了,用fsockopen要寫一堆 curl: ==============

PHP code

$file = array("upimg"=>"@E:/png.png");//檔案路徑,前面要加@,表明是檔案上傳.

$curl = curl_init("http://localhost/a.php");

curl_setopt($curl,CURLOPT_POST,true);

curl_setopt($curl,CURLOPT_POSTFIELDS,$file);

curl_exec($curl);



fsockopen: ===============

PHP code $uploadFile = file_get_contents("E:/png.png"); $boundary   = md5(time());

$postStr .="--".$boundary."\r\n";//邊界開始,注意預設比header定義的boundary多兩個'-'

$postStr .="Content-Disposition: form-data; name=\"upimg\"; filename=\"E:/png.png\"\r\n";

$postStr .="Content-Type: image/png\r\n\r\n";

$postStr .=$uploadFile."\r\n"; $postStr .="--".$boundary."\r\n";//邊界結束

write($fp,"POST /a.php HTTP/1.0\r\n");

fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n");

fwrite($fp,"Content-length:".strlen($postStr)."\r\n\r\n");

fwrite($fp,$postStr);

while (!feof($fp))

{     

echo fgets($fp, 128);

}

fclose($fp);



a.php ==============

PHP code print_r($_FILES);

聯繫我們

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