PHP用CURL發送Content-type爲application/json的HTTP/HTTPS請求

<?phpphp

$headers = array(算法

  "Content-type: application/json;charset='utf-8'", json

  "Accept: application/json", 
  "Cache-Control: no-cache", 
  "Pragma: no-cache", 
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //設置超時
$url = '這裏爲請求地址';
if(0 === strpos(strtolower($url), 'https')) {
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //對認證證書來源的檢查
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //從證書中檢查SSL加密算法是否存在
}
curl_setopt($ch, CURLOPT_POST, TRUE);
$data = array(0=>1,1=>2);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$rtn = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不設置  curl_exec返回TRUE 設置  curl_exec返回json(此處) 失敗都返回FALSE
curl_close($ch);
相關文章
相關標籤/搜索