简体   繁体   English

如何在 PHP 中的 cURL 中发送 JSON 数据

[英]How to send JSON data in cURL in PHP

I have rest API of Nodejs Server, I'm trying to make a POST call to it using PHP.我有 Nodejs 服务器的其余 API,我正在尝试使用 PHP 对它进行 POST 调用。

My php code is:我的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;
}

I have tried calling this function with diff forms of data:我试过用不同形式的数据调用这个函数:

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

OR或者

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

But on server side which Node.js, when I console log req.body I get data like this:但是在 Node.js 的服务器端,当我控制台 log req.body 时,我得到这样的数据:

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

How should I pass the data in PHP so I can get proper obj in node.js ie:我应该如何在 PHP 中传递数据,以便我可以在 node.js 中获得正确的 obj ,即:

{ '_id': '111' }

See the PHP document: http://php.net/manual/en/function.curl-setopt.php见PHP文档: http : //php.net/manual/en/function.curl-setopt.php

CURLOPT_POST : CURLOPT_POST

TRUE to do a regular HTTP POST. TRUE 执行常规 HTTP POST。 This POST is the normal application/x-www-form-urlencoded kind, most commonly used by HTML forms.此 POST 是普通的application/x-www-form-urlencoded类型,最常用于 HTML 表单。

CURLOPT_POSTFIELDS : CURLOPT_POSTFIELDS

If value is an array, the Content-Type header will be set to multipart/form-data .如果 value 是一个数组,则 Content-Type 标头将设置为multipart/form-data

So you can pass a query string returned by http_build_query() into CURLOPT_POSTFIELDS :因此,您可以将http_build_query()返回的查询字符串传递到CURLOPT_POSTFIELDS

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

and remove curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));并删除curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); . . (In fact, the varieble should be $ch , but you typed $curl , so this line doesn't work.) (实际上,变量应该是$ch ,但是您输入了$curl ,因此该行不起作用。)

In the other way, you can replace curl_setopt($ch, CURLOPT_POST, 1);另一种方式,你可以替换curl_setopt($ch, CURLOPT_POST, 1); with curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); , it can prevent the data be encoded automaticlly. ,它可以防止数据被自动编码。 And then send json_encode() data.然后发送json_encode()数据。

You have a typo in the code, which will prevent it setting the header $curl should be $ch :您在代码中有一个错字,这将阻止它设置标题$curl应该是$ch

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

You also need CURLOPT_RETURNTRANSFER uncommented.您还需要取消注释 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;
}

I have solved by using http_build_query($g, null, '&') for making data.我已经通过使用http_build_query($g, null, '&')制作数据解决了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM