cURL giving 400 in response [duplicate]
Hello to anyone willing to help.. I don’t know what else to do. I am trying to send an xml request to an api which will give back an xml formatted response. I am using curl to achieve it. But no matter what way I format my request, I always get a 400 response. The url is formatted correctly. Here is my code:
$ch1 = curl_init(); $fp1 = fopen('poi.xml','w'); curl_setopt($ch1, CURLOPT_URL, "http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=55505&minorRev=14&apiKey=xkr94zxbsjnzczryqajmjg3g&locale=en_US¤cyCode=USD&customerIpAddress=189.214.86.135&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31&xml=
"); curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch1, CURLOPT_HTTPHEADER, array('Accept: application/xml')); curl_setopt($ch1, CURLOPT_HEADER, 0); curl_setopt($ch1, CURLOPT_FILE, $fp1); curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)'); $val1 = curl_exec($ch1); $info = curl_getinfo($ch1); echo ' Dallas, TX 2 '; print_r($info); echo ''; curl_close($ch1);//Close curl session fclose($fp1); //Close file overwrite $avail = simplexml_load_file('poi.xml');
[url] => http://api.ean.com/ean-services/rs/hotel/v3/geoSearch?cid=55505&minorRev=14&apiKey=xkr94zxbsjnzczryqajmjg3g&locale=en_US¤cyCode=USD&customerIpAddress=189.214.86.135&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31&xml=Dallas, TX2 [content_type] => text/html [http_code] => 400 [header_size] => 158 [request_size] => 558 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.075402 [namelookup_time] => 0.038232 [connect_time] => 0.055687 [pretransfer_time] => 0.055787 [size_upload] => 0 [size_download] => 349 [speed_download] => 4628 [speed_upload] => 0 [download_content_length] => 349 [upload_content_length] => 0 [starttransfer_time] => 0.07531 [redirect_time] => 0
400 — Bad Request in Curl [closed]
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
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: 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:
$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 . '¤cyCode=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);
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 🙂
Php Curl HTTP/1.1 400 Bad Request
First of all you can change $output = curl_exec($ch); to echo $output = curl_exec($ch); .This way you will see error messages in your browser.
Also, try urlencode($url); in case your url has some white spaces in it.
But Your problem is most likely the security of the target server.Lots of servers treat cURL like cross site forgery attacks.This is why any cURL script must contain the csrf token of the target page to it’s post parameters.
You must first locate the token from your target page and then write code so that your script will parse the token out of the curl_exec output string.
Thanks For Replay This is the returned Value HTTP/1.1 400 Bad Request content-disposition: attachment; filename=json.json content-length: 118 content-type: application/json;charset=utf-8 date: Sat, 02 Sep 2017 12:34:26 GMT server: tsa_o strict-transport-security: max-age=631138519 vary: Origin x-connection-hash: ee1dbbca70af69c7815edcb505d16533 x-content-type-options: nosniff x-frame-options: SAMEORIGIN x-response-time: 107 x-transaction: 00d500270066af4b x-tsa-request-body-time: 1 x-xss-protection: 1; mode=block
Yes I Login To The System Via Curl And i reuse the cookies Which I Saved In files To send this request
You can always rewrite your code.
$http_headers = array( 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36',); $crl = curl_init(); $url = "http://www.example.gr/node/add/content"; curl_setopt($crl, CURLOPT_URL, $url); curl_setopt($crl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); curl_setopt ($crl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt"); curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($crl, CURLOPT_HEADER, true); curl_setopt($crl, CURLOPT_HTTPHEADER, $http_headers); curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($crl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"); curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata); echo $result=curl_exec($crl); curl_setopt($crl, CURLOPT_POST, 1); $postdata = array( "title_field[und][0][value]"=>"category", "op"=>"Save", ); curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata); echo $result=curl_exec($crl);
Many things could have gone wrong with your version.If this doesn’t work recheck what I wrote in my first anwer.
Using PHP curl how does one get the response body for a 400 response
The api I’m calling returns a json object of validation errors with the HTTP code 400. I implemented the client using PHP’s curl library, but on error curl_exec returns false. How can I get the response body on error? Note that I am setting curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
3 Answers 3
You can unset CURLOPT_FAILONERROR for once. And adding your error status code to CURLOPT_HTTP200ALIASES as expected might also help.
curl_setopt($conn, CURLOPT_FAILONERROR, false); curl_setopt($conn, CURLOPT_HTTP200ALIASES, (array)400);
(libcurl also has a CURLOPT_ERRORBUFFER , but you cannot use that option from PHP.)
Btw, curl behaves correctly by not returning the response body in case of 4xx errors. Not sure if that can be overriden. So you might have to migrate to PEAR HTTP_Request2 or a similar HTTP request class where you can deviate from the standard.
The 200 alias thing should work. I can then check the response code instead of relying on curl_exec returning false. Thanks!
«curl behaves correctly by not returning the response body in case of 4xx errors» Can you say more about that? The RFC seems to suggest otherwise: the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included entity to the user.
@Frank: The RFC recommendation pertains to servers. From an API point of view it is however undesired to return informational and unstructured results in case of errors. They are only for human consumption, it’s recommended for browsers, but very uncommon for automated user agents (fopen wrappers behave similarly). My point was just that this is expected/default behaviour of curl, not e.g. a bug.
Then let me rephrase the problem as I see it: How would one go about logging the informational error response body for consumption by the human who is troubleshooting the application that is encountering HTTP errors when connecting to a web service?
HTTP 400 Bad Request — CURL PHP
I am trying to send a PUT request using CURL, but i am getting HTTP 400 Bad Request, I am using CURLOPT_VERBOSE to debug and with that i am getting: «Error: malformed request: Expecting object found: \»email\»»> Does anybody know what this means/how i can fix? Code:
// Set the HTTP headers needed for this API call. $http_headers = array( // "Authorization: Basic xxxxxxxxxxxxxxxxxx", "Accept: application/json", "Connection: close", // Disable Keep-Alive "Expect:", // Disable "100 Continue" server response "Content-Type: application/json" // Content Type json ); // URL Endpoint of the API to be called. $ym_api_url = 'https://services.yesmail.com/enterprise/subscribers/718880'; $newsubscriber = array( 'email' => 'karina@email.com', 'firstName' => 'Karina', 'lastName' => 'McG', 'postCode' => 'BT93 EP3', 'prefersMobile' => '', 'emailFormat' => 'HTML' ); $curl_hdl = curl_init(); $curl_options = array( CURLOPT_VERBOSE => TRUE, // Verbose mode for diagnostics CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'), // Verbose output to STDERR CURLOPT_HEADER => TRUE, // Include the header in the output. CURLOPT_HTTPHEADER => $http_headers, // HTTP headers to set CURLOPT_HTTPAUTH => CURLAUTH_BASIC, // Use basic authentication. CURLOPT_USERPWD => 'xxxxxxx:xxxxxxxx', //Username/Password CURLOPT_PROTOCOLS => CURLPROTO_HTTPS, // Bitmask of protocols libcurl may use in this transfer CURLOPT_RETURNTRANSFER => TRUE, // Return result as return value of curl_exec() CURLOPT_URL => $ym_api_url, // URL to POST data to ); curl_setopt_array($curl_hdl, $curl_options); //Send array to URL curl_setopt($curl_hdl, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($curl_hdl, CURLOPT_POSTFIELDS, http_build_query($newsubscriber)); //SSL Bypass (!Temporary!) curl_setopt($curl_hdl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl_hdl, CURLOPT_SSL_VERIFYPEER, 0); // Make the API call $response = curl_exec($curl_hdl); // Get the http response code $http_response_code = curl_getinfo($curl_hdl, CURLINFO_HTTP_CODE); // Echo out the Verbose Information echo "Verbose information:\n", !rewind($verbose), htmlspecialchars(stream_get_contents($verbose)), "\n"; curl_close($curl_hdl); echo PHP_EOL . "INFO: HTTP Response Code was: " . $http_response_code . PHP_EOL; if ( $response === false ) < echo PHP_EOL . "ERROR: curl_exec() has failed." . PHP_EOL; >else