i’m connecting to a webservice using PHP and JSON. the connection is good but i want to add 2 error messages when (1) connection to the webservice is lost and (2) if webservice returns a http 500 error.
any ideas on how to do that?? i’m new to json so not sure how to do that…
this is how i connect and get data form the webservice
$url = "http://webservice.com/{$account}?loc={$loc}";
$content = file_get_contents($url);
$json = json_decode($content, true);
For error 1:
file_get_contentswill returnFALSEif what it is trying to load fails.For error 2:
Depending on what your HTTP Error 500 outputs on the page, you can simply use
file_get_contentsand look for a certain string indicating the error usingstrposand act accordingly.Reference: http://php.net/manual/en/function.strpos.php