Reputation: 17
I am Getting following error "Warning: file_get_contents failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request " while hitting "http://Google.com"
<?php
$content = file_get_contents("http://www.google.com");
if (strpos($http_response_header[0], "200")) {
echo "SUCCESS";
}
else {
echo "FAILED";
}
?>
because of this problem i am not able to parse the URL .I am using WAMP(Apache Version-2.2.22,PHP version 5.4.3).
Steps taken :- 1.allow_url_fopen Enabled in php.ini 2.Tried Encoding the URL 3.Even Used Curl php function getting same problem. 4.Allowed Firewall on
Please help.
Upvotes: 0
Views: 20888
Reputation: 1068
$html = @file_get_html($url);
if($html==FALSE) {
echo 'error';
} else {
echo 'success';
}
check it this one here $url is www.google.com.then you get the correct result
Upvotes: 5
Reputation: 350
I was having the same problem with you. I’m using WAMP on windows 7, until finally I find the solution.
You need to make sure to uncommented the extension=php_openssl.dll at php.ini file in
C:\wamp\bin\php folder. If you setup the openssl via WAMP server menu PHP >> PHP Extention, it won’t affected to the php.exe
Hope this will work for you too.
Upvotes: -1
Reputation: 30453
It is because it returns status 302 (FOUND)
echo $http_response_header[0];
// returns 'HTTP/1.0 302 Found'
Upvotes: 0
Reputation: 1242
Some server configurations do not allow opening remote addresses with file_get_contents
(via fopen
).
You might want to change the configuration, but he prefered way to open remote sites (more reliable as well as about 20x faster) is by Using cURL.
A fast and simple example can be found in the PHP docs here
Upvotes: 0
Reputation: 42925
The request actually succeeds, you have the page in $content. but you make a wrong test for success. The url does not return a 200, but a 302.
Upvotes: 2