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

我有一些PHP cURL的问题。 我试图使用PHP cURL从API获取数据。 这是我在PHP中的cURL代码:</ p>

 &lt;?php 

$ curl = curl_init();

ncurl_setopt_array($ curl,array(
CURLOPT_URL) =&gt;“https://www.example.com/dos/AW/API",
CURLOPT_RETURNTRANSFER =&gt; true,
CURLOPT_ENCODING =&gt;”“,
CURLOPT_MAXREDIRS =&gt; 10,
CURLOPT_TIMEOUT = &gt; 30,
CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST =&gt;“POST”,
CURLOPT_POSTFIELDS =&gt;“{\”过滤器\“:{\”IsActive \“:\”True \“,\ “OutputSelector \”:\“Name \”}}“,
CURLOPT_HTTPHEADER =&gt;数组(
”API_ACTION:GetItem“,
”API_KEY:MHlIARzQqxVpOg2dUxH4q9w7bx3pOL6K“,
”接受:application / json“
) ,
));

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

ncurl_close($ curl);

if($ err){
echo“cURL Error#:”。 $ err;
} else {
echo $ response;
}

?&gt;
</ code> </ pre>

使用该代码我可以得到回复但是 响应包含一些错误。 我也尝试使用 POSTMAN </ code>进行检查,并且API工作正常,因为我使用相同的数据得到了成功的响应。 我的问题是:“我的 cURL </ code>代码有什么问题可以解释为什么我在使用 cURL </ code>时遇到错误,并且我在 POSTMAN <中得到了成功的响应 / code>“?</ p>

如果有人能帮助我,我将不胜感激。
非常感谢你。</ p>
</ div>

展开原文

原文

I have some problem(s) with PHP cURL. I tried to get data from the API using PHP cURL. This is my cURL code in PHP :

<?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. 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 "?

I would appreciate if someone could help me with this. Thank you very much.

dongyumiao5210
dongyumiao5210 我经常使用它,也用于Chrome的RestletClient。但那是偏离主题;)
大约 3 年之前 回复
dsfs587465
dsfs587465 我从POSTMAN那里得到了cURL代码。我通过它生成代码。你之前曾经使用过邮差吗?
大约 3 年之前 回复
douchun2158
douchun2158 我的最后一个想法:您发布的数据无效(例如标题,身份验证......)。您还可以为用户代理发送一个http标头(邮递员会这样做?)。将邮递员请求与curl请求进行比较。抱歉。
大约 3 年之前 回复
dsdukbc60905239
dsdukbc60905239 我已经添加了curl_setopt($ch,CURLOPT_POST,1),但我仍然遇到错误
大约 3 年之前 回复
douqujin2767
douqujin2767 很难说,我不知道你发布的api。首先,将curl_setopt($ch,CURLOPT_POST,1)添加到您的代码中。
大约 3 年之前 回复
doufen5175
doufen5175 那么,我现在必须做什么呢?
大约 3 年之前 回复
douzen3516
douzen3516 对不起,我之前的评论是狗屎!!Json去了CURLOPT_POSTFIELDS。
大约 3 年之前 回复
dongye7231
dongye7231 我确切地知道这是在请求机构,因为文档说。
大约 3 年之前 回复
duanjiaolia97750
duanjiaolia97750 就像这个{“CurrentTime”:“2017-07-2109:05:25”,“xmlns:ns”:“API”,“Ack”:“错误”}
大约 3 年之前 回复
dongtanjian9310
dongtanjian9310 请告诉我们哪条错误消息;)
大约 3 年之前 回复
doubu1970
doubu1970 我刚收到错误消息作为回应
大约 3 年之前 回复
dougui2254
dougui2254 也许你可以举一些例子?
大约 3 年之前 回复
douzi2778
douzi2778 你收到什么回复代码?或者是什么错误?
大约 3 年之前 回复
drqn5418
drqn5418 请参阅stackoverflow.com/questions/13596799/...并检查您的CURLOPT_POSTFIELDS是一个字符串,如field1=value&field2=value2等。然后我认为您缺少curl_setopt($ch,CURLOPT_POST,1)来进行POST。
大约 3 年之前 回复

2个回答

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)

second, when you want a POST request, don't use CURLOPT_CUSTOMREQUEST, use 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. 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

fourth, you can hardcode the json if you want, but the code looks much prettier if you json_encode it

fifth, don't use setopt / setopt_array without checking the return type.

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. (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)

dongtan6695
dongtan6695 我弄错了。 用curl_setopt_array替换curl_setopt_array,它会起作用。
大约 3 年之前 回复
dongwei4652
dongwei4652 我用该代码获得了HTTP ERROR 500
大约 3 年之前 回复



@Antonio,你得到的响应来自另一端,可能是你遗漏了限制另一端查询处理的东西 。 尝试打印http_code,或使用curl_getinfo获取完整信息。 </ p>

如果响应代码为200,那么您可以从另一端请求验证请求。 </ p>

PS:由于回购限制无法发表评论。 </ p>
</ div>

展开原文

原文

@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. try to print http_code, or use curl_getinfo to get complete information.

in case of response code is 200, then you may ask from another end to validate the request.

PS: not able to comment because of repo restrictions.

duan1982453
duan1982453 使用CURLOPT_VERBOSE - 它提供的信息比curl_getinfo的所有选项组合的更多。
大约 3 年之前 回复
douyou2368
douyou2368 在代码下面的代码中使用以下行$ httpcode = curl_getinfo($ ch,CURLINFO_HTTP_CODE);
大约 3 年之前 回复
dongwei3712
dongwei3712 我在哪里可以打印http_code或curl_getinfo?
大约 3 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问