How do i handle error for file_get_content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kabuki1985
Forum Commoner
Posts: 32
Joined: Thu Jun 15, 2006 10:19 pm

How do i handle error for file_get_content

Post 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;
    }
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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)){
kabuki1985
Forum Commoner
Posts: 32
Joined: Thu Jun 15, 2006 10:19 pm

Post by kabuki1985 »

Thanks
Post Reply