8

I have to hit one .aspx page url from php code i am trying to hiting using curl but i am getting bellow error and there is no white space in url.

HTTP/1.1 400 Bad Request Content-Type: text/html; charset=us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Mon, 05 Oct 2015 08:31:13 GMT Connection: close Content-Length: 311 

Bellow is the code of curl which i am trying to hit.so any body will tell why iam getting this error.

$api_url = 'http://www.test/xyz/OnlineOrder.aspx?';
     $url= $api_url . 'InvoiceNo=' . $invoice;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     echo $data = curl_exec($ch);
     echo $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74

2 Answers2

11

I got the Http response 200 ok I have observed carefully the url and i found one parameter with white space that was cause the Bad request.Any way if you got some thing like problem remove the white space from url and there parameter.

using urlencode($url);

Sanjay Nakate
  • 2,020
  • 6
  • 39
  • 74
  • sometimes urlencode() doesn't produce the desired effect. In my case neither did rawurlencode(). I had to resort to str_replace to replace the spaces ONLY to get the server to return a 200. https://stackoverflow.com/questions/38995268/php-curl-bad-request-400-mapquest-geocoding – stackOverFlew Aug 17 '16 at 13:41
7

If it help anyone, my issue is I have to use POST method in curl but no POST parameters has been require, as API grab value from URL query string only. So, passing empty array in POSTFIELDS fixed my problem. My guess is it depends on Apache/PHP setting on server, whether it allows POST request without post parameters or not.

softech
  • 356
  • 1
  • 4
  • 23