user3736031
user3736031

Reputation: 19

cURL blank return

I have checked all the postings of everything I could and found no solutions to my issue. And unfortunately I am quite new to php so my knowledge is pretty slim. Therefore I pose my question:

Given the following code, why is the page blank when it should be echo'ing either the curl_error or $content?

<?php

$url = "http://thenewboston.org/";

function curl_get_contents($url)
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  $data = curl_exec($ch);
  echo curl_error($ch);
  curl_close($ch);
  return $data;
}
    $content = curl_get_contents( $url );
    echo $content;
?>

This not the specific URL I need to connect to howver at the moment I am not able to pull data from any URL I try.

Thanks.

Upvotes: 1

Views: 816

Answers (2)

Giacomo1968
Giacomo1968

Reputation: 26014

@Barmar’s answer is correct about the SSL. But here is my refactoring of your code with a few more curl options set that I use as a default in cases like this. Note the user agent is most often the biggest reason why curl attempts fail so I have CURLOPT_USERAGENT set to a standard Firefox string, but feel free to change that to be anything else you feel might be a good user agent setting:

$url = "http://thenewboston.org/";

function curl_get_contents($url) {
  $curl_timeout = 5;
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_SSLVERSION, 3);
  curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $curl_timeout);
  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
  $data = curl_exec($curl);
  curl_close($curl);
  return $data;
}

$content = curl_get_contents( $url );

echo $content;

Upvotes: 0

Barmar
Barmar

Reputation: 782498

http://thenewboston.org/ redirects to https://buckysroom.org/, and that site requires SSL version 3. Add:

curl_setopt($ch, CURLOPT_SSLVERSION, 3);

Upvotes: 4

Related Questions