简体   繁体   English

PHP cURL给出错误,但在POSTMAN中工作正常

[英]PHP cURL gives an error but works fine in POSTMAN

I have some problem(s) with PHP cURL. 我的PHP cURL有一些问题。 I tried to get data from the API using PHP cURL. 我试图使用PHP cURL从API中获取数据。 This is my cURL code in PHP : 这是我在PHP中的cURL代码:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.example.com/dos/AW/API",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"Filter\" : {\"IsActive\" : \"True\",\"OutputSelector\" : \"Name\"}}",
  CURLOPT_HTTPHEADER => array(
    "API_ACTION: GetItem",
    "API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
    "Accept: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

?>

With that code I can get a response but the response contains some errors. 使用该代码,我可以获得响应,但是响应包含一些错误。 I also tried using POSTMAN to check, and the API works fine as I got a successful response with the same data. 我也试过用POSTMAN检查,以及API正常工作,因为我得到了与同一数据成功的响应。 My question is: "Is there anything wrong with my cURL code that would explain why I got an error when I used cURL and I got successful response in POSTMAN "? 我的问题是:“我的cURL代码是否有什么错误,可以解释为什么当我使用cURL并在POSTMAN获得成功响应时出现POSTMAN ”?

I would appreciate if someone could help me with this. 我将不胜感激,如果有人可以帮助我。 Thank you very much. 非常感谢你。

given that you aren't showing us the successful postman request, we can't know for sure what errors you make, that said, you are making a couple of obvious mistakes here. 鉴于您没有向我们展示成功的邮递员请求,我们无法确定您犯了什么错误,也就是说,您在这里犯了两个明显的错误。

first off, when debugging curl code, use CURLOPT_VERBOSE , it gies you a lot of useful information when debugging your curl requests (and if you did this, you would probably notice how the Postman requests's content-type is completely different from curl's content-type http headers - more on this soon) 首先,在调试curl代码时,请使用CURLOPT_VERBOSE ,它会在调试curl请求时为您提供许多有用的信息(如果您这样做,您可能会注意到Postman请求的content-type与curl的content-type完全不同http标头-即将对此进行详细介绍)

second, when you want a POST request, don't use CURLOPT_CUSTOMREQUEST , use CURLOPT_POST . 其次,当您需要POST请求时,请勿使用CURLOPT_CUSTOMREQUEST ,而应使用CURLOPT_POST

third, when passing a string to CURLOPT_POSTFIELDS, the content-type implicitly becomes Content-Type: application/x-www-urlencoded , unless you override it. 第三,将字符串传递给CURLOPT_POSTFIELDS时,除非您将其覆盖,否则内容类型将隐式变为Content-Type: application/x-www-urlencoded and you are obviously NOT sending x-www-urlencoded data, but JSON-encoded data, so your content-type is all wrong, its supposed to be Content-type: application/json 并且您显然不是在发送x-www-urlencoded数据,而是JSON编码数据,因此您的content-type都是错误的,它应该是Content-type: application/json

fourth, you can hardcode the json if you want, but the code looks much prettier if you json_encode it 第四,您可以根据需要对json进行硬编码,但是如果您对json_encode进行编码,则代码看起来会更漂亮

fifth, don't use setopt / setopt_array without checking the return type. 第五,不要在未检查返回类型的情况下使用setopt / setopt_array。

fixing all that, you'll end up with something like: 修复所有问题后,您将得到如下结果:

function ecurl_setopt_array($ch, array $options) {
    if (! curl_setopt_array ( $ch, $options )) {
        throw new \RuntimeException ( 'curl_setopt_array failed. ' . curl_errno ( $ch ) . ': ' . curl_error ( $ch ) );
    }
}

$curl = curl_init ();

ecurl_setopt_array ( $curl, array (
        CURLOPT_URL => "https://www.example.com/dos/AW/API",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_VERBOSE => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode ( array (
                'Filter' => array (
                        'IsActive' => 'True',
                        'OutputSelector' => 'Name' 
                ) 
        ) ),
        CURLOPT_HTTPHEADER => array (
                "API_ACTION: GetItem",
                "API_KEY: MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K",
                "Accept: application/json",
                'Content-Type: application/json' 
        ) 
) );

$response = curl_exec ( $curl );
$err = curl_error ( $curl );

curl_close ( $curl );

if ($err) {
    echo "cURL Error #:" . $err;
} else {
    echo $response;
}

Edit: fixed the json data, when i wrote it, i didn't see that isActive is not an actual boolean, but the string literal True - i mistakenly encoded it as a json boolean true instead, sorry, fixed. 编辑:修复了json数据,当我编写它时,我没有看到isActive不是实际的布尔值,而是字符串文字True我错误地将其编码为json布尔值true,对不起,已修复。 (although i suspect it's supposed to be a boolean anyway, and that your original code just encodes it wrong, perhaps you should double check isActive 's type in the api docs, assuming there is one) (尽管我怀疑它还是应该是布尔值,并且您的原始代码只是将其编码为错误,但假设您有一个,则应该在api文档中仔细检查isActive的类型)

@Antonio, Response you are getting is from the other end, might be you are missing something which restrict the processing of query at other end. @Antonio,您得到的响应来自另一端,可能是您缺少限制另一端查询处理的内容。 try to print http_code, or use curl_getinfo to get complete information. 尝试打印http_code,或使用curl_getinfo获取完整的信息。

in case of response code is 200, then you may ask from another end to validate the request. 如果响应代码为200,则可以从另一端要求验证请求。

PS: not able to comment because of repo restrictions. PS:由于回购限制,无法发表评论。

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

相关问题 PHP Curl请求不起作用,但在POSTMAN中工作正常 - PHP Curl request not working but works fine in POSTMAN curl 给出 400 错误但在邮递员中工作? - curl gives 400 error but works in postman? PHP Curl POST 请求不起作用,但在 POSTMAN 中工作正常 - PHP Curl POST request not working but works fine in POSTMAN PHP CURL 给我 401 错误,但在 POSTMAN 上工作正常 - PHP CURL give me 401 error but work fine on POSTMAN PHP Soap客户端返回“ WrongVersion”错误。 在邮递员中工作正常 - PHP Soap client returning “WrongVersion” error. Works fine in Postman 获取请求与 Postman 但不能与 PHP CURL 一起工作正常 - Get Request Working fine with Postman but not with PHP CURL PHP curl问题 - 如果使用变量作为URL,则出错,但字符串工作正常 - PHP curl issue - error if using a variable as the URL, but a string works fine 为什么通过PHP cURL GET 404错误,但在浏览器中工作正常? - Why 404 error through PHP cURL GET, but works fine in browser? PHP:$ this-&gt; methodcall()给出错误,并且CalledClass:methodcall()正常工作 - PHP : $this->methodcall() gives error and CalledClass:methodcall() works fine PHP,yii框架在更新/插入时给出错误,选择效果很好 - PHP, yii framework gives error on update/insert, select works fine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM