Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

failed to open stream: HTTP request failed

Status
Not open for further replies.

DavidPlus

Programmer
Feb 20, 2007
38
NL
Hi all. I try to run the following script that read image url from rss xml and display it but when image url has space on it, it fails as shown below(trows garbage as output with error).If i copy and paste that url the image displays in the browser but for some reason it trows error on the page if it is called with this php script. I be happy if some one help me deal with image names that has space on them.Thanks

Error:

Code:
<br />
<b>Warning</b>:  imagecreatefromjpeg(./david bob.jpg): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
 in <b>/home/[URL unfurl="true"]www/script.php</b>[/URL] on line <b>27</b><br />
<br />
<b>Warning</b>:  imagecopymerge(): supplied argument is not a valid Image resource in <b>/home/[URL unfurl="true"]www/script.php</b>[/URL] on line <b>28</b><br />
‰PNG


IHDR¯ÝBµè IDATxœí½y|]Wu?ú]ç^I–5Y²[Ö`Yž;sâ8Î	$!!$…_)Bûƒ/)Ð?÷h?@[Út miûƒþZÚ$Jµ?N!vl2cÏ“dk´5[–¬ùžõþØgï³öÞçÊr—÷~ZäsÏÙ

......
.....
php Code:

Code:
<?php


	header("Cache-Control: no-cache, must-revalidate");
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  	header ("Content-type: image/png");

  	$background = imagecreatefrompng("./backround_image.png");

$xml = file_get_contents("./rss.php?xml=true");

preg_match_all('/<(artist|song|image)>([^<]+)<\/\\1>/i', $xml, $matches);

list($artist, $song, $imageurl) = $matches[2];

//echo $artist;
//echo $song;
//echo $imageurl;



  	$insert = imagecreatefromjpeg($imageurl);
  	imagecopymerge($background,$insert,47,50,0,0,80,80,100);

......
 
this
imagecreatefromjpeg(./david bob.jpg)
does not make sense against the error. so i assume you have edited the error message to protect the site reference.

your url in the xml is badly formed however, as url's cannot have spaces in them.

either make sure your url is well formed or do this

Code:
$insert = imagecreatefromjpeg(rawurlencode($imageurl));
 
japadie .I solved part of the problem(space with in name) by using :


$insert = imagecreatefromjpeg(str_replace(' ', '%20', $imageurl));

But when the name of images are like below.The script fails again. Could u help me fix these errors all together?The xml is not made by me. It is remote rss.

names that i get error:

Code:
dj%20dynamite%20&amp;%20dj%20diez.jpg

cindy &amp; stacy.jpg
 
i recommend you use the code that i posted above (using rawurlencode())
 
jpadie i followed your suggestion and now i even get error with image names that has not space too!!

Here is the error:

Code:
<br />
<b>Warning</b>: imagecreatefromjpeg(http%3A%2F%2[URL unfurl="true"]www.somesite.com%[/URL] 2Fsong_images%2bob%20marly.jpg
): failed to open stream
 
apologies. I misdirected you.

you need to split the url into its component parts and then run the rawurlencode on the path

pass the url to this function to prepare it for use

Code:
<?
function prepareURL($url) {
	// pass a url that looks like this "[URL unfurl="true"]http://www.example.com/path/to/file[/URL] with spaces.jpg";
	
	//get the path component
	$path = parse_url($url, PHP_URL_PATH);
	
	//reduce the path component to an array
	$t_path = explode('/', $path); 
	
	foreach ($t_path as $segment){
		$t_segment[] = rawurlencode($segment);
	}
	
	//recompact into string
	$t_path = implode("/", $t_segment);
	
	//insert the changed path into the url
	return str_replace($path, $t_path, $url);
}
?>
 
Thanks for your reply. you mean i call the function like this ;

prepareURL($imageurl);
$insert = imagecreatefromjpeg($imageurl);

Does this will take care of space and % & symbols?
 
nope. like this

Code:
$insert = imagecreatefromjpeg(prepareURL($imageurl));
 
I recive th following error:

Code:
<br />
<b>Warning</b>:  parse_url() expects exactly 1 parameter, 2 given in <b>/[URL unfurl="true"]www/mine2.php</b>[/URL] on line <b>34</b><br />
‰PNG

Code:
$imageurl = "[URL unfurl="true"]http://www.somesite.com/song_images/bob%20&%20smith.jpg";[/URL]

        $artist = "Andy";
        $song = "Veda";
$insert = imagecreatefromjpeg(prepareURL($imageurl));


function prepareURL($imageurl) {
    // pass a url that looks like this "[URL unfurl="true"]http://www.example.com/path/to/file[/URL] with spaces.jpg";
    
    //get the path component
    $path = parse_url($imageurl,PHP_URL_PATH);
    
    //reduce the path component to an array
    $t_path = explode('/', $path); 
    
    foreach ($t_path as $segment){
        $t_segment[] = rawurlencode($segment);
    }
    
    //recompact into string
    $t_path = implode("/", $t_segment);
    
    //insert the changed path into the url
    return str_replace($path, $t_path, $imageurl);
}

imagecopymerge($background,$insert,47,50,0,0,80,80,100);
 
you must have an old version of PHP (< 5.1.2)

try this adaptation
Code:
<?
function prepareURL($imageurl) {
    // pass a url that looks like this "[URL unfurl="true"]http://www.example.com/path/to/file[/URL] with spaces.jpg";
    
    //get the path component
    $pathinfo = parse_url($imageurl);
    $path = $pathinfo['path'];
    //reduce the path component to an array
    $t_path = explode('/', $path); 
    
    foreach ($t_path as $segment){
        $t_segment[] = rawurlencode($segment);
    }
    
    //recompact into string
    $t_path = implode("/", $t_segment);
    
    //insert the changed path into the url
    return str_replace($path, $t_path, $imageurl);
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top