0

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?

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
smftr
  • 923
  • 3
  • 17
  • 31

2 Answers2

1

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?

Community
  • 1
  • 1
Naresh
  • 2,761
  • 10
  • 45
  • 78
  • Why do you think your code should return different results? – zerkms Oct 21 '14 at 05:38
  • 1
    I am not saying that my code should return diff result. i am just giving another option and suggesting if file get content is not working. – Naresh Oct 21 '14 at 05:42
  • "and suggesting if file get content is not working" --- well, from my experience people suggest something different if it supposed to work differently. So the question is: why do you think your code is different from the OP's one? – zerkms Oct 21 '14 at 05:44
  • the example_homepage.txt is empty for me – smftr Oct 21 '14 at 08:46
0

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....

Amrinder Singh
  • 5,300
  • 12
  • 46
  • 88