smftr
smftr

Reputation: 945

file_get_contents failed to open stream when reading content from YouTube

i try to read contents from any URL with PHP. Here is my code (localhost):

$f = file_get_contents('http://www.youtube.com');

but i get this message:

failed to open stream: A conection attempt failed because the connected party did not properly respond after a period of time, or establised connection failed because connected host has failed to respond.

when i open http://www.youtube.com/ directly it's fine. So there isn't wrong with my connection. Is this failure depend on my php configuration or something else?

Upvotes: 0

Views: 1764

Answers (2)

Naresh
Naresh

Reputation: 2781

I just want to suggest cURL. cURL is best method to parse websites than file_get_contents.

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

http://php.net/manual/en/book.curl.php

http://www.sitepoint.com/using-curl-for-remote-requests/

cURL doesn’t require any configuration. some webhosts block the function files_get_contents so cURL is best every time.

Why file_get_contents is not working for this You can read SO ans.

Why doesn't file_get_contents work?

Upvotes: 1

Amrinder Singh
Amrinder Singh

Reputation: 5482

here it is:

<?php
    echo file_get_contents('http://www.youtube.com');
?>

It is working perfect in both chrome and firefox, i don't what's wrong with you? check it again....

Upvotes: 0

Related Questions