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

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!

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

Try adding a second nameserver to /etc/resolv.conf
eg: nameserver 8.8.8.8
which is the google nameserver
I found the same issue in using codebird.php. I was getting a timeout.
Our nameserver worked, but it seemed to timeout once in a while. When I added the second name server, the issue went away.

Related

WAMP : PHP Curl "working" but returning empty string, file_get_contents is working

I am running PHP Wampserver 3.2.6 under Windows 11 with Avast Antirus Free edition.
And PHP Version 8.1.0.
Now I have setup a simple curl script to fetch data from a remote host. But this returns nothing.
When I put the entire thing online on a server it works just fine. But from a local machine it doesn't work.
I have tried running the entire thing under postman. And there it works just fine.
private function __curl($url, $decode = true){
// * create curl resource
$ch = curl_init();
// * set url
curl_setopt($ch, CURLOPT_URL, $this->api_url.$url);
// * return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// * set headers
$headers = array('X-IM-API-KEY: '.$this->api_key);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, true);
// * if any postdata set
if(!empty($this->postdata)){
// * initialize post
$this->__post();
// * set postdata
curl_setopt($ch, CURLOPT_POST, count($this->postdata));
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postfieldstr);
}
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
// * if no decode
if(!$decode) return $output;
// * return result
return json_decode($output, true);
}
When I just use file_get_contents it works fine.
file_get_contents($this->api_url.$url);
The result :
{"success":false,"error":true,"message":"fields
missing","data":{"email":"not set","password":"not
set","app_version":"1.1"}}
Of course it will give an error because it expects POST parameters with the username and password.
I have the following configuration visible under PHPinfo :
I hope someone can tell me what my mistake would be.
EDIT
When I add :
curl_error($ch);
I get the following error :
SSL certificate problem: unable to get local issuer certificate
But when viewing the address in FireFox I get no error at all.
(letscrypt)
EDIT : Answer added by : #codenathan
Adding the following code to disable host and peer verification does the trick actually.
I think in combination with the local firewall the letscrypt certificate simply didn't get through in the way it was supposed to.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
Since I need this for developement purposes this actually does the trick for me.
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
However it is not ideal to switch this off : https://www.saotn.org/dont-turn-off-curlopt_ssl_verifypeer-fix-php-configuration/

PHP/Curl request is not working

phpinfo(); function shows that curl is enabled.
dll statements are also uncommented in the .ini file.
Still the curl request is not working.
<?php
//step1
$cSession = curl_init();
//step2
curl_setopt($cSession,CURLOPT_URL,"http://www.google.com");
curl_setopt($cSession,CURLOPT_RETURNTRANSFER,true);
curl_setopt($cSession,CURLOPT_HEADER, false);
//step3
$result=curl_exec($cSession);
if(curl_errno($cSession)){
echo 'Curl error: ' . curl_error($cSession);
}
//step4
curl_close($cSession);
//step5
echo $result;
?>
This code shows the error:
Curl error: Failed to connect to 192.168.1.1 port 8080: Timed out
Can you check firewall on that site. I am sure that firewall on that site is blocking traffic to IP address of your server.
Check the DNS resolution as well. Better yet, try to ping 192.168.1.1 from the machine that is not working. Also wget https://192.168.1.1:8080 (from the machine that is not working) will give you detailed error message.
use this code
function curl($url) {
$ch = curl_init(); // Initialising cURL
curl_setopt($ch, CURLOPT_URL, $url); // Setting cURL's URL option with the $url variable passed into the function
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Setting cURL's option to return the webpage data
$data = curl_exec($ch); // Executing the cURL request and assigning the returned data to the $data variable
curl_close($ch); // Closing cURL
return $data; // Returning the data from the function
}

Geoplugin API Displaying incorrect results

I'm using Geoplugin to retrieve users' current location. Everything works fine except the data is wrong. My Country is Oman but the output is USA.
Note that, I'm not using VPN or Proxy.
Code:
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$url = unserialize(file_get_contents_curl('http://www.geoplugin.net/php.gp?'.$_SERVER['REMOTE_ADDR']));
$orig_lat = $url["geoplugin_latitude"];
$orig_lon = $url["geoplugin_longitude"];
echo $orig_lat;
echo $orig_lon;
I'm using also cUrl instead of file_get_contents because allow_url_fopen in off in the server.
Output:
41.877602
-87.627197
EDIT:
I figure out that I forget to write ip in the url. http://www.geoplugin.net/php.gp?ip=
Problem SOLVED :)
Using your code returns real coordenates to my location.
Geoplugin uses MaxMind DB to get data, you can check this source.

curl "Unknown SSL protocol error in connection" error in server, but works on local

I'm trying to retrieve data from a site (i've censored the url) with this code:
<?php
$url = [doesnt really matter];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$archivo_xml = fopen("test.tst", "w");
curl_setopt($ch, CURLOPT_FILE,$archivo_xml);
curl_exec($ch);
$as1 = curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME);
$as2 = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
$as3 = curl_getinfo($ch, CURLINFO_PRETRANSFER_TIME);
$as4 = curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME);
$as5 = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
echo "Lookup: ",$as1," \n\r Connect: ",$as2," \n\r Pretransfer: ",$as3," \n\r Starttransfer: ",$as4," \n\r Total: ",$as5,"\n\r","Error: ", curl_error($ch), "\n\r";
curl_close($ch);
fclose($archivo_xml);
?>
It work's fine on local but not in the server. Here's the output from local:
Lookup: 0.015155
Connect: 0.0281
Pretransfer: 0.129087
Starttransfer: 0.786341
Total: 0.786384
Error:
and here's the output from the server:
Lookup: 0.028731
Connect: 0.043182
Pretransfer: 0
Starttransfer: 0
Total: 60.057787
Error: Unknown SSL protocol error in connection to [censored url]
With any other url works just fine, the problem is with this specific one.
localhost PHP version: 5.4.23
server PHP version: 5.5.7
Thanks in advance
Try setting cURL param
curl_setopt($ch, CURLOPT_SSLVERSION,3); // Apparently 2 or 3
SOLVED. Because of this known bug http://sourceforge.net/p/curl/bugs/1319/ I downgraded curl to 7.33 and it worked.
As in the case of this post adding curl_setopt($ch, CURLOPT_SSLVERSION,3); did not immediately resolve the issue today the SSL has been re-validated and accepted.
I had a similar situation with the same error.
Using a constant like this made it work:
curl_setopt( $handle, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3' );
Researched link: https://github.com/guzzle/guzzle/issues/1364
Other constants:
https://curl.haxx.se/libcurl/c/CURLOPT_SSLVERSION.html
Try with both options
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
If still not working then URL might be blocked on your server.

Couldn't resolve host 'maps.googleapis.com issue occurs randomly in google reverse geocoding

i tried Google reverse geocoding using PHP. If i run my PHP for first time it returns formatted address perfectly, again i reload the PHP means it returns , Curl error: Couldn't resolve host 'maps.googleapis.com'
If i again run that PHP it return address properly...That means it works randomly....
I search about this error, all said it DNS Problem, so contact DNS provider...But they said there is no problem in their side...
I tried time delay(sleep function) in between queries upto 60 seconds..but that also not works same error occurs after 60 sec delay....
My PHP Code:
<?php
//sleep(60);
$url='http://maps.googleapis.com/maps/api/geocode/json?latlng=11.49731013,77.24445245&sensor=false';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.mysite.com/dns.php');
$body = curl_exec($ch);
if($body=== false) {
echo "<br>";
echo 'Curl error: ' . curl_error($ch);
echo "<br>";
}
else
{
$json = json_decode($body);
$addr2=$json->results[0]->formatted_address;
echo "<br>";
echo $addr2;
echo "<br>";
}
?>
is it any problem in my coding....please help me solve this problem...
You could just add maps.googleapis.com to your server's hosts file, or, if you don't have access to that, connect to the IP address the address resolves to instead. Even another way would be to cache the IP address that maps.googleapis.com resolves to, and then use that cached IP address to connect in the event you get a timeout from using the FQDN.

Categories