当前位置: 代码迷 >> PHP >> php http post 怎么改写
  详细解决方案

php http post 怎么改写

热度:72   发布时间:2016-04-28 16:59:51.0
php http post 如何改写?
本帖最后由 xcvzzq 于 2015-09-16 12:30:18 编辑
如何把以下代码

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;

------解决思路----------------------
$fields = json_encode($fields);
------解决思路----------------------
   <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
  相关解决方案