0

I tried to open an external url. It works fine in my local server. But when i moved to live server it showing time out error. When i replaced the url by a url in the same domain ,it works fine.
allow_url_fopen is ON in the server.

 <?php
if ($fp = fopen('https://www.google.com/', 'r')) {
   $content = '';
   // keep reading until there's nothing left
   while ($line = fread($fp, 1024)) {
      $content .= $line;
   }

   echo $content;
   echo  'do something with the content here';
   // ...
} else {
   echo 'an error occured when trying to open the specified url';
}

?>

Updated

  $curl_handle=curl_init();
  curl_setopt($curl_handle,CURLOPT_URL,'https://www.google.co.in/');
  curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
  curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
  $buffer = curl_exec($curl_handle);
  curl_close($curl_handle);
  if (empty($buffer)){
      print "Nothing returned from url..<p>";
  }
  else{
      print $buffer;
  }

I tried cURL too. It returns "Nothing returned from url..". But it works fine in my local and demo server.

Neethu
  • 124
  • 2
  • 2
  • 11
  • If the server is a linux server and you have shell access, try to use cURL or wget from the command line and see if that works. Could it be that the server is being blocked, for some reason? Where's your server located? – M. Eriksson Dec 14 '16 at 07:30

1 Answers1

0

You should use curl to get data from third party URL. please check the below link and example : What is cURL in PHP?

Example :

$ch = curl_init();
$curlConfig = array(
    CURLOPT_URL            => "http://www.example.com/yourscript.php",
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS     => array(
        'field1' => 'some date',
        'field2' => 'some other data',
    )
);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
Community
  • 1
  • 1
prakash tank
  • 1,269
  • 1
  • 9
  • 15
  • `fopen()` is perfectly valid if you just want to read the contents of a URL. He can _try_ with curl and see if it works, but it's not a matter of _should_ or _shouldn't_. – M. Eriksson Dec 14 '16 at 06:54
  • @MagnusEriksson : Yes sir. I agreed. But CURL is a right and followed way to get access and retrieve data from third party URL. – prakash tank Dec 14 '16 at 07:02
  • @Neethu : check another link : – prakash tank Dec 14 '16 at 07:03
  • ive tried this code with curl $curl_handle=curl_init(); curl_setopt($curl_handle,CURLOPT_URL,'https://www.google.co.in/'); curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2); curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($curl_handle); curl_close($curl_handle); if (empty($buffer)){ print "Nothing returned from url..

    "; } else{ print $buffer; } and it gives nothing returned from url .

    – Neethu Dec 14 '16 at 07:05
  • The issue at hand is most likely not cURL vs not cURL. Like I said. `fopen()` is _perfectly valid_ if you're just doing a simple GET request to fetch the contents of a URL. cURL is recommended for more complex requests (sending headers, other request types like POST/PUT/DELETE etc ). and when you need a more fine grained control. – M. Eriksson Dec 14 '16 at 07:08
  • @prakashtank : i tried the example u given. It return a blank page in my production server and works fine in my local – Neethu Dec 14 '16 at 07:15
  • @MagnusEriksson ive checked this code in local server and other project servers too. And it works fine in all of them . I cannot find the whats the real issue with this server. – Neethu Dec 14 '16 at 07:19
  • @Neethu : Please check that curl is installed or not on your production server. Thanks – prakash tank Dec 14 '16 at 07:40
  • @prakashtank : cURL is installed. Otherwise it will show the error 'Sorry cURL is not installed!' when i run this code http://www.jonasjohn.de/snippets/php/curl-example.htm – Neethu Dec 14 '16 at 08:31