php 400 - Curl 中的错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13764623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:01:42  来源:igfitidea点击:

400 - Bad Request in Curl

phpcurl

提问by user7282

It seems like im getting a problem when Im trying to send a request to a server in php. The http api request is something like this:

当我尝试在 php 中向服务器发送请求时,我似乎遇到了问题。http api 请求是这样的:

http://api.ean.com/ean-services/rs/hotel/v3/avail?minorRev14&apiKey=p9ycn9cxb2zp3k3gfvbf5aym&cid=55505&locale=en_US&hotelId=122212&stateProvinceCode=%20NV%C2%A4cyCode=USD&arrivalDate=12/27/2012&departureDate=12/28/2012&room1=2,&room2=2,18,15&room3=3,16,16,15&room4=3,&includeDetails=true&includeRoomImages=true

http://api.ean.com/ean-services/rs/hotel/v3/avail?minorRev14&apiKey=p9ycn9cxb2zp3k3gfvbf5aym&cid=55505&locale=en_US&hotelId=122212&stateProvinceCode=%20NV%C2%A4cyCode=USD&arrivalDate=12/27/2012&departureDate=12/28/2012&room1=2,&room2=2,18,15&room3=3,16,16,15&room4=3,&includeDetails=true&includeRoomImages=true

I have the following part of my code in php where I believe the error is occuring:

我在 php 中有我的代码的以下部分,我相信错误正在发生:

$url = 'http://api.ean.com/ean-services/rs/hotel/v3/avail?minorRev14';
$url .= '&apiKey=p9ycn9cxb2zp3k3gfvbf5aym';
$url .= '&cid=55505';
$url .= '&locale=' . $locale . '&hotelId=' . $hotelid . '&stateProvinceCode=' . $state . '&currencyCode=USD';

$url .= '&arrivalDate=' . $datefr . '&departureDate=' . $dateto . '&' . $details . '&includeDetails=true&includeRoomImages=true';
$header = "Accept: application/json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));
$ee       = curl_getinfo($ch);
print_r($ee);

print_r($retValue);

This is what I get from the print_r(curl_getinfo($ch)) statement:

这是我从 print_r(curl_getinfo($ch)) 语句中得到的:

Array (
    [url] => http://api.ean.com/ean-services/rs/hotel/v3/avail?minorRev14&apiKey=p9ycn9cxb2zp3k3gfvbf5aym&cid=55505&locale=en_US&hotelId=122212&stateProvinceCode= NV¤cyCode=USD&arrivalDate=12/27/2012&departureDate=12/28/2012&room1=2,&room2=2,18,15&room3=3,16,16,15&room4=3,&includeDetails=true&includeRoomImages=true
    [content_type] => text/html
    [http_code] => 400
    [header_size] => 181
    [request_size] => 340
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.469
    [namelookup_time] => 0
    [connect_time] => 0.125
    [pretransfer_time] => 0.125
    [size_upload] => 0
    [size_download] => 349
    [speed_download] => 744
    [speed_upload] => 0
    [download_content_length] => 349
    [upload_content_length] => 0
    [starttransfer_time] => 0.469
    [redirect_time] => 0
    [certinfo] => Array ( )
    [redirect_url] =>
)

I'm not sure what the solution can be and I have been looking at this problem for awhile now hopefully someone can help me thanks :)

我不确定解决方案是什么,我已经研究这个问题一段时间了,希望有人能帮助我,谢谢:)

回答by user7282

add

添加

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

This way it tells it what type of agent (browser, spider, etc) is requesting the content.

通过这种方式,它告诉它什么类型的代理(浏览器、蜘蛛等)正在请求内容。

So finally, I modified your code to

所以最后,我修改了你的代码

<?php 
$url ='http://api.ean.com/ean-services/rs/hotel/v3/avail?minorRev14&apiKey=p9ycn9cxb2zp3k3gfvbf5aym&cid=55505&locale=en_US&hotelId=122212&stateProvinceCode=%20NV%C2%A4cyCode=USD&arrivalDate=12/27/2012&departureDate=12/28/2012&room1=2,&room2=2,18,15&room3=3,16,16,15&room4=3,&includeDetails=true&includeRoomImages=true';

$header = array("Accept: application/json");

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

$retValue = curl_exec($ch);
$response = json_decode(curl_exec($ch));
$ee       = curl_getinfo($ch);
print_r($ee);

print_r($retValue);
?>

and it is working fine for me. Can you also check?

它对我来说很好用。你也可以检查吗?

回答by Arcadien

There is something weird in :

有一些奇怪的东西:

&stateProvinceCode=%20NV%C2%A4cyCode=USD

&stateProvinceCode=%20NV%C2%A4cyCode=USD

                 ^^^^^^^^^^^^^^^^^^^

Converting it to plain ascii, its "NV¤cyCode"

将其转换为纯 ascii,其“NV¤cyCode”

From my pov it should be like

从我的观点来看,它应该是这样的

&stateProvinceCode=NV&cyCode=USD

&stateProvinceCode=NV&cyCode=USD

Just as information, what kind of error did you get from server?

就像信息一样,你从服务器得到什么样的错误?