Forum Moderators: coopster
$contents = file_get_contents("http://www.example.com/whatever.xml"); $streamContext = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false
]
]);
$contents = file_get_contents("http://www.example.com/whatever.xml", false, $streamContext); // This one worked yesterday, but doesn't now
// $file = "http://w1.weather.gov/xml/current_obs/KUKF.xml";
// This one does work, though
$file = "https://news.google.com/news/feeds?output=rss";
$contents = file_get_contents($file);
echo $contents;
PHP 5.6.30 (cli) (built: Feb 2 2017 04:17:08)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend Guard Loader v3.3, Copyright (c) 1998-2014, by Zend Technologies $streamContext = stream_context_create([
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true,
'verify_depth' => 0
]
]);
$contents = file_get_contents($file, false, $streamContext); HTTP request failed! HTTP/1.1 400 Bad Request
if (!$contents = file_get_contents("http://w1.weather.gov/xml/current_obs/KUKF.xml")) {
$error = error_get_last();
echo $error['message'];
}
else echo "It worked!"; $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $file);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);
echo $contents;
// Using the weather.gov link, cURL gives me:
HTTP/1.1 403 Forbidden
Server: AkamaiGHost
Mime-Version: 1.0
Content-Type: text/html
Content-Length: 303
Expires: Fri, 03 Feb 2017 21:48:02 GMT
Date: Fri, 03 Feb 2017 21:48:02 GMT
Connection: close function file_get_contents($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$contents = curl_exec($ch);
curl_close($ch);
return $contents;
}