Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents() how to fix error "Failed to open stream", "No such file"

Tags:

php

https

I'm getting the following error when I try to run my PHP script:

failed to open stream: No such file or directory in C:\wamp\www\LOF\Data.php on line 3 script:

My code is as follows:

<?php  $json = json_decode(file_get_contents('prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=*key*'));  print_r($json);  ?> 

Note: *key* is a replacement for a string in the URL (my API key) and has been hidden for privacy reasons.

I removed the https:// from the URL to get one error to disappear.

Am I doing something wrong here? Maybe the URL?

like image 394
Life of Madness Avatar asked Dec 13 '13 08:12

Life of Madness


People also ask

What is the use of file_get_contents () function?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

What is the difference between file_get_contents () function and file () function?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.

What does file_get_contents return?

This function is similar to file(), except that file_get_contents() returns the file in a string, starting at the specified offset up to length bytes. On failure, file_get_contents() will return false . file_get_contents() is the preferred way to read the contents of a file into a string.

How do I echo content in PHP?

The file_get_contents function takes the name of the php file and reads the contents of the text file and displays it on the console. get the contents, and echo it out. <? php echo file_get_contents( "filename.


2 Answers

The URL is missing the protocol information. PHP thinks it is a filesystem path and tries to access the file at the specified location. However, the location doesn't actually exist in your filesystem and an error is thrown.

You'll need to add http or https at the beginning of the URL you're trying to get the contents from:

$json = json_decode(file_get_contents('http://...')); 

As for the following error:

Unable to find the wrapper - did you forget to enable it when you configured PHP?

Your Apache installation probably wasn't compiled with SSL support. You could manually try to install OpenSSL and use it, or use cURL. I personally prefer cURL over file_get_contents(). Here's a function you can use:

function curl_get_contents($url) {   $ch = curl_init($url);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);   $data = curl_exec($ch);   curl_close($ch);   return $data; } 

Usage:

$url = 'https://...'; $json = json_decode(curl_get_contents($url)); 
like image 124
Amal Murali Avatar answered Sep 16 '22 13:09

Amal Murali


Why don't you use cURL ?

$yourkey="your api key"; $url="https://prod.api.pvp.net/api/lol/euw/v1.1/game/by-summoner/20986461/recent?api_key=$yourkey"; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  $auth = curl_exec($curl); if($auth) { $json = json_decode($auth);  print_r($json); } } 
like image 42
Shankar Narayana Damodaran Avatar answered Sep 20 '22 13:09

Shankar Narayana Damodaran