PHP用CURL发送Content-type为application/json的HTTP/HTTPS请求

<?php

$headers = array(

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

  "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);
posted @ 2017-12-01 11:00  去.玩儿  阅读(16616)  评论(0编辑  收藏  举报