0

My shipping company has an "API" which instructs me to use file_get_contents() in order to receive a shipping quote.

$zipcode=1234;
$volume=2;

$shipping_quote=file_get_contents(http://www.shipping.com?z=$zipcode&v=$volume);

Occasionally, their server will be down and I will receive no response. Is there someway to check if file_get_contents has made a connection? I am getting "failed to open stream errors". Instead of these, I would like to be able to present my users with a meaningful response "Server busy, Try again".

Matthew A
  • 491
  • 4
  • 11
  • 1
    possible duplicate of [file\_get\_contents failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error](http://stackoverflow.com/questions/21971800/file-get-contents-failed-to-open-stream-http-request-failed-http-1-1-500-inter) , also take a look at [workaround](http://stackoverflow.com/questions/3535799/file-get-contents-failed-to-open-stream) – Naruto Mar 11 '15 at 13:08
  • `if ($shipping_quote === FALSE) { /* Download failed */ }`. – Phylogenesis Mar 11 '15 at 14:29

1 Answers1

0

Try like this

 //initialize curl
$URL = "http://www.shipping.com";
$curlInit = curl_init($URL);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
//check for response
$response = curl_exec($curlInit);
curl_close($curlInit);

if ($response) 
{
    $shipping_quote=file_get_contents("http://www.shipping.com?z=$zipcode&v=$volume");
}else
{
    $shipping_quote="";
}
Raddy T
  • 9
  • 2