简体   繁体   English

Node.js无法通过php读取curl请求的数据

[英]Node js can not read the data from curl request via php

I have a problem to read the data I sent via curl request in nodejs.我在 nodejs 中读取通过 curl 请求发送的数据时遇到问题。 With Postman everything is working properly, but when I try to send request via curl, the api return 500 internal server error and I don't know where is the mistake.使用 Postman 一切正常,但是当我尝试通过 curl 发送请求时,api 返回 500 内部服务器错误,我不知道错误在哪里。

so here is my php code所以这是我的 php 代码

private function curl($url, $post = false, $header = false)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
    }
    curl_setopt($ch, CURLOPT_HEADER, [
        'Content-Type: application/json',
        'Accept: application/json',
        'Content-Length: ' . mb_strlen(json_encode($post))
    ]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec ($ch);
    curl_close($ch);
    return $response;
}

so now I will share with you the part of nodejs所以现在给大家分享一下nodejs的部分

console.log(req.body)
    let content = req.body.content
    if (!content.length) {
        return res.status(401).send({message: 'content is required'})
    }

and here is the log info这是日志信息

{ '{"content":"content information"}': '' }

2020-08-18T07:27:38.191100+00:00 app[web.1]: TypeError: Cannot read property 'length' of undefined. 2020-08-18T07:27:38.191100+00:00 app[web.1]: TypeError: 无法读取未定义的属性“长度”。 I don't know where is my mistake, and why I can not read json data on node.js, any help will be appriciate.我不知道我的错误在哪里,为什么我无法读取 node.js 上的 json 数据,任何帮助都会很合适。

your code has 2 notable bugs, and due to PHP's shitty libcurl wrappers, php doesn't even warn you when it notices,您的代码有 2 个值得注意的错误,并且由于 PHP 糟糕的 libcurl 包装器,php 在发现时甚至不会警告您,

 curl_setopt($ch, CURLOPT_HEADER, [

CURLOPT_HEADER decides whether or not curl should print the received headers along with the body in the output, and it does not even accept arrays, it accepts bools (true/false/0/1), if the api was well-designed, you would get an InvalidArgumentException or TypeError when giving CURLOPT_HEADER anything other than a bool.. but because it's not well designed, php just... "converts the array to a bool" (meaning it will be false if the array is empty, or true otherwise) CURLOPT_HEADER decides whether or not curl should print the received headers along with the body in the output, and it does not even accept arrays, it accepts bools (true/false/0/1), if the api was well-designed, you would给 CURLOPT_HEADER 提供除 bool 以外的任何内容时,会得到InvalidArgumentExceptionTypeError 。但由于它的设计不佳,php 只是...“将数组转换为布尔值”(这意味着如果数组为空,它将为false ,否则为true )

what you actually wanted here is CURLOPT_HTTPHEADER , the option for setting HTTP request headers, taking an array.您真正想要的是CURLOPT_HTTPHEADER ,用于设置 HTTP 请求标头的选项,采用数组。

furthermore,此外,

    'Content-Length: ' . mb_strlen(json_encode($post))

this is wrong, first off mb_strlen() gives you the number of unicode characters in the string, while the Content-Length header is supposed to contain the number of bytes , not the number of unicode characters, so it should be strlen(), not mb_strlen() - secondly, curl will add this header automatically if you don't add it, and curl won't make any typos, and won't calculate the length wrong, so you're better off just letting curl add this header automatically anyway (you calculated the length wrong, curl won't make that mistake, also humans are prone to introducing typos, curl has testsuites to make sure there are no typos in the "Content-Length" header, by comparison, i bet y这是错误的,首先 mb_strlen() 为您提供字符串中 unicode 字符的数量,而Content-Length header 应该包含字节数,而不是 Z8AB3B19E134F01FBAF94B8E1 not mb_strlen() - secondly, curl will add this header automatically if you don't add it, and curl won't make any typos, and won't calculate the length wrong, so you're better off just letting curl add this header automatically anyway (you calculated the length wrong, curl won't make that mistake, also humans are prone to introducing typos, curl has testsuites to make sure there are no typos in the "Content-Length" header, by comparison, i bet是的our project does not have testsuites to make sure you didn't introduce any typos in the Content-Length header)我们的项目没有测试套件来确保您没有在Content-Length标头中引入任何拼写错误)

I faced the same problem while trying to do this.我在尝试执行此操作时遇到了同样的问题。 This can be solved by using http_build_query($post) instead of json_encode .这可以通过使用http_build_query($post)而不是json_encode来解决。 If you're using express JS make sure you have below handlers.如果您使用的是 express JS,请确保您有以下处理程序。

app.use(express.urlencoded({ extended: false }));
app.use(express.json());

It would appear that you are not properly adding the body to your request in php/curl.看来您没有正确地将正文添加到 php/curl 中的请求中。 As a result, req.body.content is undefined .结果, req.body.contentundefined Accessing a property on undefined throws your error, so you need to check for that possibility in your JS code, but the real issue in your system is that you aren't sending a body in php.访问undefined的属性会引发错误,因此您需要在 JS 代码中检查这种可能性,但系统中的真正问题是您没有在 php 中发送正文。 Hopefully that helps you get to the root of the problem and you can figure out how to attach the body data to your request from there.希望这可以帮助您找到问题的根源,并且您可以从那里弄清楚如何将正文数据附加到您的请求中。

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

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