curl和php Curl

最近在研究php项目,构想出做一个发短信的平台。这和在java web项目中用到的短信接口一样。所以需要用到php的Curl。

废话不说,直接贴代码:


$arr = array ("mobilePhoneNumber"=>"电话号码","content"=>"发送内容");
$data = json_encode($arr);
$curl = curl_init('https://api.bmob.cn/1/requestSms');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//设置头部header的内容
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "X-Bmob-Application-Id: appid",
    "X-Bmob-REST-API-Key: apikey"
    )
);

$ret = curl_exec($curl);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
echo $httpCode; 
echo curl_error($curl);
curl_close($curl);
?>

但运行之后,发现返回的状态码为“0”,请求根本没有发送到服务器,输出错误内容发现“SSL certificate problem: self signed certificate in certificate chain”错误。最后通过加入下面两句修改了错误:

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);

整一个代码为以下:


$arr = array ("mobilePhoneNumber"=>"电话号码","content"=>"发送的内容");
$data = json_encode($arr);
$curl = curl_init('https://api.bmob.cn/1/requestSms');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "X-Bmob-Application-Id: appid",
    "X-Bmob-REST-API-Key: apikey"
    )
);

$ret = curl_exec($curl);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
echo $httpCode; 
echo curl_error($curl);
curl_close($curl);
?>

你可能感兴趣的:(php)