Reputation: 598
The mentioned link in variable $link is redirect to product page. Its working fine when i call/open it in browser. But it does not work in PHP file_get_contents function.
My Code:
$url = "750651";
$link = "http://www.costco.com/CatalogSearch?storeId=10301&catalogId=10701&langId=-1&keyword=$url";
$link = str_replace('&','&',$link);
$res = file_get_contents(html_entity_decode(urldecode($link)));
Error
Warning: file_get_contents(http://www.costco.com/CatalogSearch?storeId=10301&catalogId=10701&langId=-1&keyword=750651): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
How can I prevent conversion of &
into &
in file_get_contents functions
I also have tried following code but no success
$link = "http://www.costco.com/CatalogSearch?";
$options = array("storeId"=>"10301","catalogId"=>"10701","langId"=>"-1","keyword"=>$url);
$link .= http_build_query($options,'','&');
$res = file_get_contents($link);
Upvotes: 2
Views: 4913
Reputation: 129
I used it this way:
$myURL = 'http://www.costco.com/CatalogSearch?';
$options = array("storedId"=>$10301,"câtlogId"=>10701,"langId"=>-1,"keyword"=>$url);
$myURL .= http_build_query($options,'','&');
$myData = file_get_contents("$myURL");
And it ran well. Try with this.
Upvotes: 1
Reputation: 2780
Try it without the urldecode and entity_decode, or do those before the string_replacement
$link = "http://www.costco.com/CatalogSearch?storeId=10301&catalogId=10701&langId=-1&keyword=$url";
$link = str_ireplace('&','&', html_entity_decode(urldecode($link)));
$res = file_get_contents($link);
You cannot get every site via file_get_contents, this because of the same-origin policy. The site owner should open up Access-Control-Allow-Origin for that to work. You can however download the site via CURL, that goes something like this:
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // if you want to follow redirects
$data = curl_exec($ch);
curl_close($ch);
Upvotes: 0
Reputation: 598
I also found alternative function to do this. I hope this would be use full.
function get_fcontent( $url, $javascript_loop = 0, $timeout = 5 ) {
$url = str_replace( "&", "&", urldecode(trim($url)) );
$cookie = tempnam ("/tmp", "CURLCOOKIE");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $ch, CURLOPT_ENCODING, "" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); # required for https urls
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
if ($response['http_code'] == 301 || $response['http_code'] == 302) {
ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
if ( $headers = get_headers($response['url']) ) {
foreach( $headers as $value ) {
if ( substr( strtolower($value), 0, 9 ) == "location:" )
return get_url( trim( substr( $value, 9, strlen($value) ) ) );
}
}
}
if ( ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) && $javascript_loop < 5) {
return get_url( $value[1], $javascript_loop+1 );
} else {
return array( $content, $response );
}
}
To see results
$lurl=get_fcontent($link);
echo $lurl[0];
Source https://stackoverflow.com/a/5402193/3466544
Upvotes: 1