HTTP Error 400. The request is badly formed - php

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;

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;

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
answer of cURL having issues handling URL Source with colons.

You need to encode your URL before sending the request
<?php
$url=urlencode("http://domain/file name.xml");
?>
urlencode

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.

Related

new line in url causing bad request - invalid url when using php curl

I have the following php:
$ch = curl_init("http://domain/blah/".rawurlencode("foo bar"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
print_r ($result);
curl_close($ch);
That works as it should, but when I change foo bar to foo\r\nbar or foo\nbar or drop the rawurlencode and just send foo%0D%0Abar or foo%0Abar, I get a Bad Request - Invalid URL error.
If I try:
$ch = curl_init("http://domain/blah/foo\r\nbar");
I get no error, but nothing happens.
I can put some other characters in the url and have them replaced with newlines, but that seems a bit of a workaround. I'm looking for the right way to do this.
I'm using php 5.6.7

urlencode & SQL request does not work... (cUrl and html special chars seems not to work)

First part of the question solved. Please look at EDIT 2
I have the following problem:
I get a username out of my database with a htmlspecialchar, in this case: exámple. I parse it via cUrl to the Riot-Games API.
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/exámple?api_key=<my-api-key>'
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
return $output;
curl_close($ch);
My result is: { "status": { "message": "Not Found", "status_code": 404 } }
But the user exists! I can open the URL in my browser and it works...
I think cUrl can't parse the á in the string, if i copy the url and open it in browser
á is converted to %C3%A1
Is it somehow possible to convert the URL in PHP to a sendable request?
Thanks in advance ;)
EDIT
If i use ex%C3%A1mple instead of exámple in my user-database it works!
EDIT 2
I now use urlencode to the username. However I'm despairing...
My simple script:
echo urlencode($row["username"]) . " " . urlencode('exámple');
The $row["username"] outputs me guaranteed 'exámple'. It's an SQL request.
The output i get:
ex%E1mple ex%C3%A1mple
Solved:
urlencode(utf8_encode($row["username"]))
use urlencode function to encode name. See below code:
$name='exámple';
$encoded_name = urlencode('exámple'); //output: ex%C3%A1mple
$url = 'https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/'.$encoded_name.'?api_key=<my-api-key>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;

HTML entity '&times' and php function

I need a function that return me the Timezone of a specific location, so i use the
Google Time Zone API.
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'&timestamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
The function doesn't work because if i return $url i can see that GET variable "&timestamp=" is transformed into "×tamp=".
If i run the script outside the function it works.
WHY??
----UPDATE----
I resolved the problem, the curl doesn't work with https://, so i add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
See this for more information PHP cURL Not Working with HTTPS
The function works fine. The reason you're seeing ×tamp= is because &times is being converted to ×. If you view source you'll see the correct url(instead of viewing the converted entity on the web page).
Why ; is not required
There is no problem with this function. If you echo that URL you will get the multiplication sign because it is being filtered through html and recognizing the ascii code. This only happens when you view it though and html viewer (browser), if you view source you will see the original string.
To confirm that this conversion will not occur when passed through curl_setopt(), I ran your code on my server and got an expected result.
echo timezoneLookup(52.2023913, 33.2023913);
function timezoneLookup($lat, $lng){
$url = 'https://maps.googleapis.com/maps/api/timezone/json?location='.$lat.','.$lng.'&timestamp='.time().'&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
Returned...
{ "dstOffset" : 3600, "rawOffset" : 7200, "status" : "OK", "timeZoneId" : "Europe/Kiev", "timeZoneName" : "Eastern European Summer Time" }
If this code is not working for you then it could be a networking issue. Try doing curl with another webpage and see what happens. Also, with a simple api call like this you could easily use file_get_contents()

'file_get_contents' The contents are encrypted?

The problem is when i use file_get_contents to get source (HTML) from this site, the result that i receive is NOT a plain html code.
The code i used:
$source = file_get_contents("http://mp3.zing.vn/bai-hat/Dance-With-My-Father-Luther-Vandross/ZWZ9D6FD.html");
echo $source;
// OR print_r($source);
The source i received:
��}{�#Ǒ��-��!E��=��Mv�5�B���R�����h��E�HV7YE�������a�X��p{��[�:�!{��;,v��u��Or��̬��Y��M��ʌ̌�����������F��ޖ����ػ��S� #�~��H�7k�����ʎȦ2���M?�ު&D�����t���$u�O��N���>%(Y����I��Vb�[���VN�=�[�![*�dE*�]3:�ޑ�xiA���Z��g ��祇VejI �R�y�֨�ea��o��s�M/�... *MORE
I tried with cURL, but i also received the same result:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://mp3.zing.vn/bai-hat/Dance-With-My-Father-Luther-Vandross/ZWZ9D6FD.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$source = curl_exec($ch);
curl_close($ch);
I think the source i received must have been encrypted, but if i use browser to view source, the source will NOT be encrypted.
Eventually, i dont really know what happened, and how to get the plain source (plain HTML) ?
It's gzip compressed, just set the correct encoding and you're good to go
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://mp3.zing.vn/bai-hat/Dance-With-My-Father-Luther-Vandross/ZWZ9D6FD.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
$source = curl_exec($ch);
curl_close($ch);
Take a look at gzdecode (requires the ZLIB PHP module, though - if you don't have it, I'd strongly consider to use JimL's method using cURL).
string gzdecode ( string $data [, int $length ] )
$source = file_get_contents("http://mp3.zing.vn/bai-hat/Dance-With-My-Father-Luther-andross/ZWZ9D6FD.html");
echo gzdecode($source);
// OR print_r($source);

sending xml string using php curl but not as post parameter

:)
I'm trying to send an XML using curl but not as post parameter. what I mean is this.
for example.
the receiving side of that XML won't be able to recieve the XML using $_POST variable.
he will need to use the following code:
$xmlStr=null;
$file=fopen('php://input','r');
$xmlStr=fgets($file);
I want to be able to send an xml string using curl via https.
so the following would be wrong:
public static function HttpsNoVerify($url,$postFields=null,$verbose=false) {
// Initialize session and set URL.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($postFields !=null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
if ($verbose) {
curl_setopt($ch, CURLOPT_VERBOSE, 1);
}
// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
because here i can use HttpsNoVerify($url,array('xml_file'=>'xml..')); and that
will paste it as post parameter. and i want it as post output.
so please I hope i explained myself properly and I explained exactly what I don't want to do.
how can I do what i want to do?
thanks! :)
kfir
Just directly pass the xml string as second parameter instead of an associative array item,
HttpsNoVerify($url, 'xml ..');
This will eventually call
curl_setopt($ch, CURLOPT_POSTFIELDS, "xml ...");
Which will be put in php://input for the remote server.

Categories