Hidde Hidde - 12 days ago 5
PHP Question

cUrl error: "Couldn't resolve host 'search.twitter.com' "

When trying to connect to the Twitter api using cUrl and PHP, I get the following error:

Couldn't resolve host 'search.twitter.com'

Before it worked fine, and the request works in my browser. When trying to connect to example.com, I do get the page. I've also tried the following code, with which I get the same error:

$_h = curl_init();curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, 'http://www.example.com' );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );

var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h));


Could this be a DNS issue? Are there any other options for fetching JSON data with PHP?

EDIT: This is the code that got an error initially:

$json_url = "https://search.twitter.com/search.json?q=" . 'bitterrific' . "&rpp=10&result_type=all";
$ch = curl_init();
$timeout = 100;
curl_setopt($ch,CURLOPT_URL,$json_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
//curl_close($ch);
$content = json_decode($data);
$output = array();
echo "DATA: ".curl_error($ch);


Thanks!

Answer

I didn't found a problem with your code.

Also search.twitter.com works:

$ host search.twitter.com
search.twitter.com is an alias for s.twitter.com.
s.twitter.com has address 199.59.148.11
s.twitter.com has address 199.59.149.243
s.twitter.com has address 199.59.148.84

You should check your server's DNS settings.

You also ask for other ways to fetch json data with PHP. PHP accepts urls to be used as remote files. So you could try something like:

$content = file_get_contents("https://search.twitter.com/search.json?q=" . 'bitterrific' . "&rpp=10&result_type=all");
$content = json_decode($content);
var_dump($content);
Comments