versha
versha

Reputation: 107

Curl giving 503 Service Unavailable in response

My URL "www.example.com" is working in browser but when I get response via curl of URL "www.example.com" I get 503 service unavailable response.

I used the following code:

 $url = 'http://www.example.com';
   $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);  
    curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
    curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
    $JsonResponse = curl_exec($curl_handle);
    $http_code = curl_getinfo($curl_handle);
    print_r($http_code);die;

Upvotes: 7

Views: 14034

Answers (1)

CodeZombie
CodeZombie

Reputation: 5377

I'm pretty sure the remote server requires specific HTTP headers (cookies for example), like a session token or a language preference.

You have to analyze the HTTP traffic sent from your browser to the remote server and find the required HTTP headers yourself. I recommend a tool like Fiddler.

An example:

GET / HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: foo=bar
Connection: keep-alive

Assuming the remote server requires clients to send a cookie with the name foo, he will probably send you a 503 or 400 error message in the case you omit it. You have to send the cookie from cURL as well in order to get a successful response, acting like a regular client.

Upvotes: 1

Related Questions