php http post 如何改写?

xcvzzq 2015-09-16 12:29:55
如何把以下代码

curl -H "Content-type: application/json" -X POST -d '{
"aaa": "aaa"
"bb": "bb",
"cc": {
"cc1": "cc1",
"cc2": "cc3"
}
}' "http://xxx"


转为php post代码?

写的如下,不成功


$fields = array(
"aa" => "aa",
"bb" => "bb"
);

$response = http_post_fields("http://xxx", $fields);
echo $response;
...全文
138 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
不止鱼 2015-09-17
  • 打赏
  • 举报
回复
用curl啊
不止鱼 2015-09-17
  • 打赏
  • 举报
回复
   <html>
 <head>
  <title>PHP 测试</title>
 </head>
 <body>
<?php
$fields = array(
  "aa"  =>  "aa",
   "bb"  =>  "bb"
);

$fieldsdate = json_encode($fields);

$ch = curl_init("http://xxx");
curl_setopt($ch, CURLOPT_HEADER, "Content-type: application/json");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsdate);
$output = curl_exec($ch);
if(curl_errno($ch)){//出错则显示错误信息
    print curl_error($ch);
}

curl_close($ch);
echo $output;


?>
</body>
</html>
PHP如何使用http curl 传输数据 http://www.paymoon.com/index.php/2015/09/17/how-to-use-php-execute-curl/#phpcurl
码无边 2015-09-17
  • 打赏
  • 举报
回复

<?php
$post_data = 
			array(
					'cclist=5AAAA',
					'date=0',
					'irname=',
					'fullpath=',
			);

	$post_data = implode('&',$post_data);

	$url='http://mathuat.us/check/shopavon.php';

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_URL,$url);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	ob_start();
	curl_exec($ch);
	$result = ob_get_contents() ;
	ob_end_clean();

echo $result;


?>

xuzuning 2015-09-16
  • 打赏
  • 举报
回复
$fields = json_encode($fields);
放胆往前冲 2015-09-16
  • 打赏
  • 举报
回复
json_encode($value);
xcvzzq 2015-09-16
  • 打赏
  • 举报
回复
是这样的串,jason后的结果是相同的,然而还是不成功。 但是curl就成功了 所以我试图打印echo $response;,但是没什么结果,如何跟踪post请求啊?好定位错误?
引用 3 楼 xuzuning 的回复:
你那 -d 不是 这样的串吗?'{ "aaa": "aaa" "bb": "bb", "cc": { "cc1": "cc1", "cc2": "cc3" } }'
xuzuning 2015-09-16
  • 打赏
  • 举报
回复
你那 -d 不是 这样的串吗?'{ "aaa": "aaa" "bb": "bb", "cc": { "cc1": "cc1", "cc2": "cc3" } }'
xcvzzq 2015-09-16
  • 打赏
  • 举报
回复
谢谢,还是不成功, 打印echo $response; 不出任何东西
引用 楼主 xcvzzq 的回复:
如何把以下代码

curl -H "Content-type: application/json" -X POST     -d '{    
     "aaa": "aaa"
     "bb": "bb",
     "cc": {
       "cc1": "cc1",
       "cc2": "cc3"
     }
   }'     "http://xxx"
转为php post代码? 写的如下,不成功


$fields = array(
  "aa"  =>  "aa",
     "bb"  =>  "bb"
);

$response = http_post_fields("http://xxx", $fields);
echo $response;

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧