Page 1 of 1

How do i handle error for file_get_content

Posted: Mon Aug 07, 2006 4:25 am
by kabuki1985
I want to use the function (portion from googlemap PHP class) to return me geocode values based on the location. But i want to be able to catch error if the location is not valid.

Warning: file_get_contents(http://api.local.yahoo.com/MapsService/ ... =asdasdasd) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in

My fetch url portion is as below:

Code: Select all

<?php
    function fetchURL($url) {
		$aContext = array( 
					'http'=> array(
						'proxy'=>'tcp://proxy.myproxy.com:8080',
						'request_fulluri' => True,
						),
					);
		$cxContext = stream_context_create($aContext);

		if(!$content = file_get_contents($url,False,$cxContext)){
			echo "Not valid";
			//throw new CustomException('Unable to read file contents',self::FILE_DATA_ERROR);
		}else
			return $content;
    }
?>

Posted: Mon Aug 07, 2006 4:34 am
by JayBird
supress the error by putting an @ symbol infrom of the function call like so

Code: Select all

@file_get_contents($url,False,$cxContext)){

Posted: Mon Aug 07, 2006 4:42 am
by kabuki1985
Thanks