前言:
现在兄弟们对“curlphp”大体比较看重,看官们都需要知道一些“curlphp”的相关文章。那么小编也在网上收集了一些有关“curlphp””的相关知识,希望朋友们能喜欢,同学们一起来了解一下吧!使用出curl发送body时,需要加header 'Content-Type: application/json' ,否则content type就是application/x-www-form-urlencoded。特分享以下两种方法可以将数据输出成json格式
<?php
$data_string = json_encode(array(
'subject'=>'demo',
'content'=>'测试数据'
));
//curl验证成功
$ch = curl_init("");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
echo $result;
接上面的curl依然可以访问成功
//curl验证成功
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data_string);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8'
)
);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
curl_close($curl);
echo $res;
标签: #curlphp