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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using special characters in a URL with fopen() 1

Status
Not open for further replies.

Robbo79

IS-IT--Management
Nov 27, 2006
21
0
0
GB
I'm trying to open the following URL with fopen:

Ceramic Hob&bq=[price(float GBP)>=159.00 GBP]&max-results=3&orderby=[x=price(float GBP): max(x)]

(copy and paste the entire line, it will work in a browser).

However, trying:

Code:
<?php
$url = "[URL unfurl="true"]http://www.google.com/base/feeds/snippets/-/products?q=Electric[/URL] Ceramic Hob&bq=[price(float GBP)>=159.00 GBP]&max-results=3&orderby=[x=price(float GBP): max(x)]";

fopen($url,'rb');
?>

Results in the following warning:

PHP Warning: fopen( Ceramic Hob&bq=[price(float GBP)>=159.00 GBP]&max-results=3&orderby=[x=price(float GBP): max(x)]): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request

Thinking the special characters were the cause of the problem, I tried:

Code:
<?php
$url = "[URL unfurl="true"]http://www.google.com/base/feeds/snippets/-/products"[/URL] . urlencode("?q=Electric Ceramic Hob&bq=[price(float GBP)>=159.00 GBP]&max-results=3&orderby=[x=price(float GBP): max(x)]");
?>

fopen($url,'rb');
?>

However, I receive the same error (although the special characters are now encoded).

The URL works fine in a browser, and I can open other external URL's with fopen(), but I cannot seem to get this particular URL (or any variation of it) to work.

I have checked allow_url_fopen is enabled and the user_agent value is set to mimic a known web browser in php.ini. I doubt either to be the problem though as mentioned earlier I can open other external URL's with fopen().
 
this should work

Code:
<?php
$url = '[URL unfurl="true"]http://www.google.com/base/feeds/snippets/-/products?';[/URL]

$qs['q'] = 				'Electric Ceramic Hob';
$qs['bq'] = 			'[price(float GBP)>=159.00 GBP]';
$qs['max-results'] = 	3;
$qs['orderby']=			'[x=price(float GBP): max(x)]';

$queryString = array();
foreach ($qs as $key=>$val){
	$queryString[] = urlencode($key) .'='.urlencode($val);
}

$activeURL = $url . implode ('&amp;', $queryString);
$results = file_get_contents ($activeURL);
echo htmlspecialchars($results);
?>

basically you need to encode each key and each value separately. and then assemble them after encoding.

file_get_contents is also a useful alternative to fopen/fread. also cURL is even more adaptable.

the resultant feed is a messy xml concoction. a csv output is much easier to parse in php.
 
Thanks jpadie, that worked a treat.

The feed is returned as an Atom feed (hence the XML) - as I'm only reformatting parts of it to HTML the XML isn't much of an issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top