php下使用curl進行多種資料編碼方式的POST請求____編碼

來源:互聯網
上載者:User

php使用curl請求資料是很常見的,但是根據HTTP/1.1 協議下的POST提交資料編碼方式的不同,使用curl函數參數的選擇也是有所區別的。
請求報文頭header中的 Content-Type標記著傳輸的編碼方式供服務端識別,以下根據Content-Type的不同正確使用curl傳輸資料

一.application/x-www-form-urlencoded方式:

1.普通類似web表單資料:
curl方法:

public function curl_post($data, $url) {    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    $tmpInfo = curl_exec($ch);    if (curl_errno($ch)) {    echo 'Errno' . curl_error($ch);    }    curl_close($ch);    return $tmpInfo;}

處理樣本:

請求:

public function test2(){                    $template = array(            'touser' => 'test1',            'template_id' => '11',            'url' => 'http://www.test.com',            'topcolor' => '#EEEEEE',            'data' => array('t1','t2','t3')        );        $data = $template;           $url = "http://127.0.0.1/jytest/test1";        $this->curl_post($data, $url);        }

接收處理:

public function test1(){            $res = $_POST;      $content = '';      $content = $content.$res['touser'].', ';      $content = $content.$res['template_id'].', ';      $content = $content.$res['url'].', ';      $content = $content.$res['topcolor'].', ';      $content = $content.serialize($res['data']).', ';      $filePath = "./test.txt";            file_put_contents($filePath,$content);    }

test.txt檔案內容是: test1, 11, http://www.test.com, #EEEEEE, s:5:”Array”;,

可以看出 提交的數組無法正確接收
如果要傳數組,則要將curl_post方法中的

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

改為

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

2.傳json,沿用上面的curl_post方法不變,只是將json內容當做字串內容傳遞,要有個頭部欄位名與之對應,方面接收

處理樣本:

public function test2(){    //請求方    $template = array(        'touser' => 'test1',        'template_id' => '11',        'url' => 'http://www.test.com',        'topcolor' => '#EEEEEE',        'data' => array('t1','t2','t3')    );    $data['data'] = json_encode($template);    $url = "http://127.0.0.1/test/test1";    $res =  $this->curl_post($data, $url);    print_r(json_decode($res,1)); }

接收處理:

public function test1(){     //接收處理返回      $data = json_decode($_POST['data'],true);          $content=serialize($data);      $filePath = "./test.txt";            file_put_contents($filePath,$content);        }

test.txt檔案內容是:
a:5:{s:6:”touser”;s:3:”test1”;s:11:”template_id”;s:2:”11”;s:3:”url”;s:19:”http://www.test.com“;s:8:”topcolor”;s:7:”#EEEEEE”;s:4:”data”;a:3:{i:0;s:2:”t1”;i:1;s:2:”t2”;i:2;s:2:”t3”;}}
可以看出是數組$template的內容

二.直接application/json方式:

curl設定header將Content-Type改為application/json

curl方法

private function curl_postjson($data,$urlcon){        $ch = curl_init($urlcon); //請求的URL地址        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON類型字串        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data)));        $tmpInfo = curl_exec($ch);        curl_close($ch);        return $tmpInfo;    }

請求:

public function test2(){            //請求方      $template = array(            'touser' => 'test1',            'template_id' => '11',            'url' => 'http://www.test.com',            'topcolor' => '#EEEEEE',            'data' => array('t1','t2','t3')        );      //不需要用單獨的欄位名對應json數組,服務端要用file_get_contents('php://input')接收      $data = json_encode($template);        $url = "http://127.0.0.1/test/test1";          $res = $this->curl_postjson(json_encode($data), $url);      print_r(json_decode($res,1));    }

接收處理:

public function test1(){           $filePath = "./test.txt";         //不需要使用欄位名來接收json數組      $res =  file_get_contents('php://input');       $res = json_decode($res,1) ;      $content = serialize($res);      file_put_contents($filePath,$content);      echo json_encode($res);    }

test.txt檔案內容是:
s:110:”{“touser”:”test1”,”template_id”:”11”,”url”:”http:\/\/www.test.com”,”topcolor”:”#EEEEEE”,”data”:[“t1”,”t2”,”t3”]}”;

聯繫我們

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