Harish Kumar
Harish Kumar

Reputation: 39

HTTP Error 400. The request is badly formed

I am getting the same error for this, please suggest

$url="http://domain.com/manage/File Name.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;

Upvotes: 3

Views: 29812

Answers (4)

Aatif Khan
Aatif Khan

Reputation: 328

Answers here suggest using url_encode or curl_escape functions. If you want to be RFC 3986 compliant, use rawurlencode() function instead. This helped with a curl request in PHP 7. Hoep it helps others.

Upvotes: 1

Deepika Patel
Deepika Patel

Reputation: 2701

This error come, when your curl url contains white spaces. you have to encode url for remove white space.

$base_url = "http://domain.com/manage/";
$url = "File Name.xml";
$ch = curl_init();
$final_url = $base_url . curl_escape($ch, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);                
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;

Upvotes: 6

Édouard Lopez
Édouard Lopez

Reputation: 43401

As describe in the comment, your URL contains not encoded characters (spaces).

Solution

Encode your URL when setting CURLOPT_URL:

curl_setopt($ch, CURLOPT_URL, urlencode($url));

You could also use curl_escape() to encode the query string part.

References

Upvotes: 3

tim
tim

Reputation: 3359

You need to encode your URL before sending the request

<?php
$url=urlencode("http://domain/file name.xml");
?>

urlencode

Upvotes: 1

Related Questions