Hidde Hidde - 1 year ago 93
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 );


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;
$data = curl_exec($ch);
$content = json_decode($data);
$output = array();
echo "DATA: ".curl_error($ch);


Answer Source

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
s.twitter.com has address
s.twitter.com has address

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download