【问题标题】:How to send JSON data in cURL in PHP如何在 PHP 中以 cURL 发送 JSON 数据
【发布时间】:2018-12-21 21:11:52
【问题描述】:

我有 Nodejs 服务器的其他 API,我正在尝试使用 PHP 对其进行 POST 调用。

我的php代码是:

function post_url($apiRoute,$data) {
    $request_url = 'http://test-app.herokuapp.com';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url . $apiRoute);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_POST, 1);
    echo $data ;
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

我试过用不同形式的数据调用这个函数:

$g = array("_id" => "111");
$postapiresponse = post_url('/CCTRequest/get',json_encode($g));

$postapiresponse = post_url('/CCTRequest/get',json_encode(array("_id" => "111"));

但是在 Node.js 的服务器端,当我控制台日志 req.body 时,我得到这样的数据:

{ '{"_id":"111"}': '' }

我应该如何在 PHP 中传递数据,以便在 node.js 中获取正确的 obj,即:

{ '_id': '111' }

【问题讨论】:

  • 不要手动对数据进行 json_encode,CURLOPT_POSTFIELDS 需要一个数组。
  • @castis 我试过发送数组。当我在服务器上发送这个数组(“_id”=>“111”)时,我在 req.body 中得到 {}
  • @castis curl_setopt($ch, CURLOPT_POSTFIELDS, array("_id" => "111"));试过这个。在服务器中获取 {}
  • 请在 cmets 中分享 cURL

标签: php node.js curl post


【解决方案1】:

查看 PHP 文档: http://php.net/manual/en/function.curl-setopt.php

CURLOPT_POST

TRUE 以执行常规 HTTP POST。这个帖子很正常 application/x-www-form-urlencoded 种类,最常用于 HTML 表单。

CURLOPT_POSTFIELDS

如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data

所以你可以将http_build_query()返回的查询字符串传递给CURLOPT_POSTFIELDS

post_url('/CCTRequest/get', http_build_query($g, null, '&'));

并删除curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));。 (其实变量应该是$ch,但是你输入了$curl,所以这行不行。)

另外,你可以将curl_setopt($ch, CURLOPT_POST, 1);替换成 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');,这样可以防止数据被自动编码。然后发送json_encode() 数据。

【讨论】:

    【解决方案2】:

    您的代码中有错字,这将阻止它设置标题 $curl 应该是 $ch:

    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

    您还需要取消注释 CURLOPT_RETURNTRANSFER。

    function post_url($apiRoute, $data) {
        $request_url = 'www.example.com';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request_url . $apiRoute);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        curl_close($ch);
    
        return $result;
    }
    

    【讨论】:

    • 累了你的ans但没有成功
    • 我们可以看看你的节点代码吗? Works fine in my tests,但不反对你的herokuapp,得到{"status":404,"content":"_id is required"}
    【解决方案3】:

    我已经用http_build_query($g, null, '&')制作数据解决了。

     $g = array("_id" => "111");
    $g = http_build_query($g, null, '&');
    $postapiresponse = post_url('/CCTRequest/get', $g);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-11
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-31
      • 2012-06-20
      相关资源
      最近更新 更多