1

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.

6
  • Are you running it on an external server? I am only running it on Xampp thought I wouldn't think that would matter Commented Jun 13, 2014 at 0:49
  • Please make youre you have php_curl installed. Prepend something like echo 'curl Extension is: '.( function_exists('curl_version') ? 'present' : 'not installed or disabled'); to your script Commented Jun 13, 2014 at 0:51
  • 1
    ran on ubuntu vm. try adding error_reporting(E_ALL); in the beginning Commented Jun 13, 2014 at 0:51
  • I do have curl installed and enabled, version 7.36.0 for reference. Tried the error_reporting(E_ALL); with no result :P Commented Jun 13, 2014 at 0:54
  • Any reason you're not using file_get_contents()? Does your server have fopen_url disabled? Commented Jun 13, 2014 at 1:05

2 Answers 2

4

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

curl_setopt($ch, CURLOPT_SSLVERSION, 3);
Sign up to request clarification or add additional context in comments.

Comments

0

@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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.