YouTube API v3 not working [duplicate] - php

This question already has answers here:
file_get_contents(): SSL operation failed with code 1, Failed to enable crypto
(21 answers)
Closed 4 years ago.
I am developing a web page and I have a small problem with the YouTube API. I want to get all the information of any YouTube video, but I can not get the data. I have the following PHP code to make the request to the server:
Here is my Code:
$video_get_contents = file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet&id=J643lwa-BlM&key=AIzaSyATfzheNPybhDYRIgLwQw__rjE8XddZP8s');
$video_data = json_decode($video_get_contents);
$video_data_title = $video_data->items[0]->snippet->title;
echo $video_data_title;
When I run the previous code I do not get anything, and I do not know why. If I access the URL specified in the code, the JSON code is generated correctly.
When executing my code I get these errors:
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in C:\xampp\htdocs\beta.php on line 4
Warning: file_get_contents(): Failed to enable crypto in C:\xampp\htdocs\beta.php on line 4
Warning: file_get_contents(https://www.googleapis.com/youtube/v3/videos?part=snippet&id=J643lwa-BlM&key=AIzaSyATfzheNPybhDYRIgLwQw__rjE8XddZP8s): failed to open stream: operation failed in C:\xampp\htdocs\beta.php on line 4
Could you tell me if the PHP code is correct?

you cant access https url with the file_get_contents,
however for youtube API you can access it using the example in https://developers.google.com/youtube/v3/code_samples/php
and if you still want to access the json in your url, here is a similar question about how to access https with file get contents file_get_contents(): SSL operation failed with code 1. And more

Related

file_get_contents cannot get a resourse over https [duplicate]

This question already has answers here:
How to get file_get_contents() to work with HTTPS?
(12 answers)
Closed 5 years ago.
I'm trying to run the following code to get the html of a web page:
$html = file_get_contents("https://www.bet365.com");
But I get these warnings:
PHP Warning: file_get_contents(): SSL:
PHP Warning: file_get_contents(https://www.bet365.com): failed to open stream: HTTP request failed!
I've tried setting stream context with different headers, but cannot find a workig solution. What might be the reason for this warning and how can I solve it.
This was an enormously helpful link to find:
http://php.net/manual/en/migration56.openssl.php
An official document describing the changes made to open ssl in PHP 5.6 From here I learned of one more parameter I should have set to false: "verify_peer_name"=>false
Similar post Visit it

Trying to access url that returns json results in connection reset by peer error

My php web app is trying to access another URL on the same site that returns a json payload. My web app is using file_get_contents() on the url that returns the json and then does something with it.
I have tried using curl instead of file_get_contents, but i get the same errors which are:
A PHP Error was encountered
Severity: Notice
Message: file_get_contents(): send of 24 bytes failed with errno=104 Connection reset by peer
Filename: xxxxxx.php
Line Number: 46
And
A PHP Error was encountered
Severity: Notice
Message: file_get_contents(): send of 2 bytes failed with errno=32 Broken pipe
Filename: xxxx.php
Line Number: 46
Now, this worked on the previous server the web app was hosted on i'm told and this is now on some shared hosting.
So, to recap, when you visit the url http://example.com/getjson it runs some php that does:
$foo = file_get_contents('http://example.com/twitter.json/?user_id=xxxxxx&count=500&include_rts=1&page=1&include_entities=5');
If i visit http://example.com/twitter.json/?user_id=xxxxxx&count=500&include_rts=1&page=1&include_entities=5 in my browser i see the json, if i try and get it with php, i get the errors above.
I just can't think what to try next!
Any help gratefully received!

file_get_contents() -> SSL operation failed with code 1

I had this piece of code to get the likes of a particular Facebook page in production since years:
$url = 'https://graph.facebook.com/<facebook site id>';
echo json_decode(file_get_contents($url))->{'likes'};
Now we upgraded to PHP 5.6 and it stopped working with the following error message:
Warning: file_get_contents() [function.file-get-contents.php]: SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in <some page> on line 182
Warning: file_get_contents() [function.file-get-contents.php]: Failed to enable crypto in <some site> on line 182
Warning: file_get_contents(https://graph.facebook.com/<facebook site id>) [function.file-get-contents.php]: failed to open stream: operation failed in <some page> on line 182
Is there a simple way to fix this?
I know there is a referenced answer but this answer disables SSL verification and I don't want to implement a possible security hole!
CURLOPT_SSL_VERIFYPEER is set to TRUE by default starting with cURL 7.10
You could set it to FALSE again, although it would be wiser to set the CA certificates (see http://php.net/manual/en/function.curl-setopt.php for more info).
Also, keep in mind that file_get_contents will return FALSE on timeout, so it may be a good idea to :
- Set a lower timeout than the default php timeout of 0 seconds
- Handle any returns of FALSE

Remove Facebook Fans using Facebook API: file_get_contents(): SSL operation failed with code 1

I recently found out that I have a huge amount of Facebook bots that are following my Facebook Fanpage. As a result I have lots of trouble interacting with my fans.
Thus I wanted to to remove all the bots from my page using that instruction:
Facebook API: Get fans of / people who like a page
Unfortunately when I use that code I get an error message:
Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Applications/XAMPP/xamppfiles/htdocs/schnooglestandalone/test.php on line 19
Warning: file_get_contents(): Failed to enable crypto in /Applications/XAMPP/xamppfiles/htdocs/schnooglestandalone/test.php on line 19
Warning: file_get_contents(http://www.facebook.com/plugins/fan.php?connections=100&id=251682891644217): failed to open stream: operation failed in /Applications/XAMPP/xamppfiles/htdocs/schnooglestandalone/test.php on line 19
This answer didnt help:
file_get_contents(): SSL operation failed with code 1. And more
As you can see, I am running the script from a local XAMPP Server. My Facebook page is german.
Thanks guys

YouTube API implementation with SimpleXML no longer working

I had a website that pulled videos from a user's account using a public XML feed. The feed's URL is:
https://gdata.youtube.com/feeds/api/users/USERNAME/uploads
Where USERNAME is dynamically assigned via a PHP variable. The code to hit this URL and grab the contents is:
$file = file_get_contents('https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads');
$xml = simplexml_load_file($file);
However, this is now throwing a weird error:
Warning: file_get_contents() [function.file-get-contents]: Couldn't
resolve host name in /home/dpw/public_html/inc/modules/youtube.php on
line 46
Warning:
file_get_contents(https://gdata.youtube.com/feeds/api/users/dpwrestling/uploads)
[function.file-get-contents]: failed to open stream: operation failed
in /home/dpw/public_html/inc/modules/youtube.php on line 46
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity "" in
/home/dpw/public_html/inc/modules/youtube.php on line 47
Warning: Invalid argument supplied for foreach() in
/home/dpw/public_html/inc/modules/youtube.php on line 49
I'm not sure what the problem is, as visiting the URL in my web browser returns the XML fine. Why is my PHP script now having trouble retrieving it?
For the record, the allow_url_fopen is still enabled in my hosting environment as I checked using echo ini_get('allow_url_fopen'), which printed 1.

Categories