José Ricardo
José Ricardo

Reputation: 1489

Why did I get a 400 Bad Request when getting a page with PHP Curl?

I was trying to access a page using curl. I could access it perfectly using the browser and using "static" strings as the URL, like:

$url = "http://www.example.com/?q=1234"

But when I tried to access the page using a variable in the URL string like:

$url = "http://www.example.com/?q=$param"

I got a 400 error code. I checked out on the web and found some comments here in this stackoverflow thread:

Then, just for curiosity I did the following:

$url = "http://www.example.com/?q=" . trim($param);

and it worked! And no, $param did NOT contain any spaces.

To me, it seems that it can be some encoding error, but I really can't find an explanation for it. Does anyone here in stackoverflow know what it can possibly be?

Thanks in advance :)

Upvotes: 0

Views: 1833

Answers (3)

ChrisLively
ChrisLively

Reputation: 88044

You're best bet is to echo out the $url variable after assignment. That way you can see exactly what is in it.

Upvotes: 0

Pekka
Pekka

Reputation: 449385

Well, if you're 100% sure it didn't contain a space, then $param probably contained one of the other characters trim() cuts away with:

  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NUL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.

You could find out for sure by using ord():

$string = "\nTEST\r";
for ($i = 0; $i <= strlen($string)-1;  $i++) 
 echo "(".ord($string[$i]).")";

this snippet will output all character values in the string, including those of invisible characters:

 (10)(84)(69)(83)(84)(13)

the 10 beingh the newline character in the beginning of the string.

Upvotes: 3

CodeOverload
CodeOverload

Reputation: 48465

Maybe the website reject external requests, so maybe try adding a referrer value as the website and a client browser value as firefox. maybe that can trick it and accept the request.

Upvotes: 0

Related Questions