
php curl post json数据字符串什么写
2016-09-16 · 知道合伙人互联网行家
关注

展开全部
在我的博客《PHP cURL实现模拟登录与采集使用方法详解》中“十一、发送与获取json数据”对这个问题做了详细讲解,下面是示例代码:
<?php
#json数据
$url = 'http://test.com/curl/testPostJsonData.php';
$data = '{"a":"b"}'; //数据格式1:直接json格式类型数据
$length = strlen($data);
$header = array(
'Content-Length: ' . $length, //不是必需的
'Content-Type: text/json',
);
$ch = curl_init($url); //初始化curl
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$content = curl_exec($ch); //执行并存储结果
curl_close($ch);
echo $content;
#数据格式2:\n分割数据
$data = [
'name:Zjmainstay',
'website:http://www.zjmainstay.cn',
];
$data = implode("\n", $data);
#数据格式3:&分割数据
$data = 'name:Zjmainstay&website:http://www.zjmainstay.cn';
文章还涉及很多php curl相关内容,如需了解,请访问查看。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询