Error fetching data from youtube in xml format - php

I am trying to fetch youtube videos using Youtube Data API URL. Whenever i paste the URL directly in the address bar, it perfectly shows the result. But whenever i try to use that URL in simplexml_load_file() function, i get errors. I cannot figure out what is going on.
This is how i am doing this.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = simplexml_load_file($url);
echo "<pre>";
print_r($output);
echo "</pre>";
I am getting this error message.
Warning: simplexml_load_file(): SSL: The operation completed successfully. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 38 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 25 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Notice: simplexml_load_file(): send of 2 bytes failed with errno=10054 An existing connection was forcibly closed by the remote host. in J:\xampp\htdocs\youtube\tube.php on line 23
Warning: simplexml_load_file(https://gdata.youtube.com/feeds/api/videos/-/cat): failed to open stream: HTTP request failed! in J:\xampp\htdocs\youtube\tube.php on line 23
Warning: simplexml_load_file(): I/O warning : failed to load external entity "https://gdata.youtube.com/feeds/api/videos/-/cat" in J:\xampp\htdocs\youtube\tube.php on line 23
I tried to use file_get_contents() function it also gives error.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = file_get_contents($url);
echo $output;
I get these errors using above code.
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: No such host is known. in J:\xampp\htdocs\youtube\tube.php on line 15
Warning: file_get_contents(https://gdata.youtube.com/feeds/api/videos/-/cat): failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in J:\xampp\htdocs\youtube\tube.php on line 15
I also tried to use simplexml_load_string() function it also gives error.
$url = "https://gdata.youtube.com/feeds/api/videos/-/cat";
$output = simplexml_load_string($url);
echo "<pre>";
print_r($output);
echo "</pre>";
The above code gives me these errors.
Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in J:\xampp\htdocs\youtube\tube.php on line 14
Warning: simplexml_load_string(): https://gdata.youtube.com/feeds/api/videos/-/cat in J:\xampp\htdocs\youtube\tube.php on line 14
Warning: simplexml_load_string(): ^ in J:\xampp\htdocs\youtube\tube.php on line 14
I am using XAMPP v3.2.1 to run my script.
Please help. Thanks.

First you have to check that remote file access is enabled, read the PHP manual page about Using remote files.
After that, and if your firewall is not blocking connections, almost any option that you tried should work. But, in any case, here you have one using simpler commands, or at least simpler concerning what is working internally. Just a good old fopen.
// initialize variables
$url = 'https://gdata.youtube.com/feeds/api/videos/-/cat';
$content = false;
$handle = false;
// open the url/file
$handle = fopen( $url, 'r' );
// get the content from streaming
$content = stream_get_contents($handle);
// close the file reading session
fclose( $handle );
var_dump( $content );

Related

Why some access modes in fopen() throw a PHP warning on my new server?

I am doing a PHP script migration from php 5.6.40 (Apache 2.2.15 CentOS 6.10 server) to php 8.0.1 (Apache 2.4.37 CentOS 8.3).
I have troubles with fopen() function which throws a warning according to the mode option.
I searched the web without finding similar problem...
On each server, I have the same context and the same permissions :
$ pwd
/export/php/logs/aita
$ ll
-rw-r--r--. 1 apache apache 33 27 janv. 15:30 testFopen.txt
My test-script is very simple, it cheks all fopen modes :
<?php
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r'); // line 2
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'r+'); // line 3
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w'); // line 4
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'w+'); // line 5
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a'); // line 6
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'a+'); // line 7
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x'); // line 8
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'x+'); // line 9
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c'); // line 10
$handle = fopen("/export/php/logs/aita/testFopen.txt", 'c+'); // line 11
When running this script, I obtain :
On my old server PHP 5.6.40
[error] [client x.x.x.x] PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 8
[error] [client x.x.x.x] PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): failed to open stream: File exists in /export/html/aita_dev/testFopen.php on line 9
Thoses warnings are logicals (file exists and exclusion).
On my new server PHP 8.0.1
[proxy_fcgi:error] [pid 37251:tid xxxx] [client x.x.x.x:x] AH01071: Got error '
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 3
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 4
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 5
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 8
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: File exists in /var/www/html/aita_dev/testFopen.php on line 9
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 10
PHP message: PHP Warning: fopen(/export/php/logs/aita/testFopen.txt): Failed to open stream: Permission denied in /var/www/html/aita_dev/testFopen.php on line 11'
Warnings from lines 8 and 9 make sense here too.
Modes r+, w[+] and c[+] generate the "permission denied" warning.
Modes r, a[+] works well, without warnings.
I am able to read the file with "r" option.
I am able to write the file with "a" and "a+" options so it doesn't seem like a permission problem.
I would like to understand and not have to correct all the fopen() of my applications...
The administrator in charge of the servers has lowered the alert level of SELinux (Security-Enhanced Linux) and everything is now functioning normally.
The system was set up to only allow read/append on files for the Apache user as suggested by IMSoP

Simple HTML DOM Parser error: "Warning: file_get_contents()"

I was just starting to test use Simple HTML DOM Parser to parse google.com, while getting the following error:
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/Ampps/www/scraper/simplehtmldom_1_9_1/simple_html_dom.php on line 82
Warning: file_get_contents(): Failed to enable crypto in /Applications/Ampps/www/scraper/simplehtmldom_1_9_1/simple_html_dom.php on line 82
Warning: file_get_contents(https://google.com): failed to open stream: operation failed in /Applications/Ampps/www/scraper/simplehtmldom_1_9_1/simple_html_dom.php on line 82
And my scraper_test.php looks like this:
include('./simplehtmldom_1_9_1/simple_html_dom.php');
$html = file_get_html('https://google.com');
echo $html->find('title', 0)->plaintext;

libxml_use_internal_errors(true) doesn't remove all warnings

I want to GET a XML feed using simplexml_load_file() and I want to handle the errors by myself. The problem is, libxml_use_internal_errors(true) does not remove all the warnings. Here is my exact code:
<?php
libxml_use_internal_errors(true);
$url = "http://feeds.feedburner.com/GoogleOnlineSecurityBlog";
$res = simplexml_load_file($url);
When I purposefully write a typo in the URL, I get these warnings:
Warning: simplexml_load_file(): php_network_getaddresses: getaddrinfo failed: Unknown host. in C:\xampp\htdocs\test.php on line 4
Warning: simplexml_load_file(http://feeds.feedburener.com/GoogleOnlineSecurityBlog): failed to open stream: php_network_getaddresses: getaddrinfo failed: Unknown host. in C:\xampp\htdocs\test.php on line 4
Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://feeds.feedburener.com/GoogleOnlineSecurityBlog" in C:\xampp\htdocs\test.php on line 4 error
Adding or removing libxml_use_internal_errors(true) only adds/removes the last warning and not the first 2. What did I do wrong? I have PHP 7.2.2.
Thank you for your help.

Connection timed out in simple_html_dom.php

Error message:
PHP Warning:
file_get_contents(https://search.rakuten.co.jp/search/mall/%E3%81%B5%E3%82%8B%E3%81%95%E3%81%A8%E7%B4%8D%E7%A8%8E/):
failed to open stream: Connection timed out in /root/apps/kpi_tool_poc/simple_html_dom.php on line 76, referer: http://test-dash.local/kpi_tool_poc/new_trend_word.php
The error message tells that there is not finding the file you want to open. Try to create an if with file_get_contents and after that to process the file

file_get_contents not working in ubuntu and windows

i am having a problem with PHP file_get_contents.i am trying to fetch inforamtion following url but is not getting information.it's showing following error.i have try curl code also but same error is displayed
PHP Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages:
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure in /home/tshah/public_html/pramod/expressdecorViolation/classes/scrap1.php on line 83
PHP Warning: file_get_contents(): Failed to enable crypto in /home/tshah/public_html/pramod/expressdecorViolation/classes/scrap1.php on line 83
PHP Warning: file_get_contents(http://www.overstock.com/Home-Garden/Kraus-30-inch-Undermount-Single-Bowl-Steel-Kitchen-Sink/3381330/customer-reviews.html?rsort=1): failed to open stream: operation failed
mycode is:
$link = 'http://www.overstock.com/Home-Garden/Kraus-30-inch-Undermount-Single-Bowl-Steel-Kitchen-Sink/3381330/customer-reviews.html?rsort=1';
$Page_information = file_get_contents($link);
print_r($Page_information);

Categories