boboodz
asked on
cURL not woring.
I have a piece of code that uses cURL that used to work perfectly before. The uRL works well in any browser and returns the values I require. However cURL is giving an error 'couldn't connect to host'
The only changes that have occurred since then is that imap has been installed and a few directories have had their permissions changed. The weird thing is that the URL works in any browser. can you help please?
The only changes that have occurred since then is that imap has been installed and a few directories have had their permissions changed. The weird thing is that the URL works in any browser. can you help please?
Any changing done to the network setting such as sub net-mask, iptables Firewalls, added any timeout to your script regarding execution time out(); check permission also.
It is possible your browsers are using a proxy & gaining access to the site via the proxy. A simple connection test is to use telnet to attempt to connect to port 80. Open a terminal and type:
telnet <site> 80
Where <site> is the domain of the URL. So, for google.com: telnet google.com 80
If it connects, just use CTRL+] to exit the connection and then type quit to exit telnet.
telnet <site> 80
Where <site> is the domain of the URL. So, for google.com: telnet google.com 80
If it connects, just use CTRL+] to exit the connection and then type quit to exit telnet.
I'm assuming you're using it for an http download? To help troubleshooting:
- Can you use telnet ("telnet <domain or ip> 80")?
- Can you use wget ("wget <url.")?
- Can you ping the host? ("ping <domain>")
- Are your DNS servers working well? (check /etc/resolv.conf)
Has anything changed in the firewall (transparent proxy or the like)?
- Can you use telnet ("telnet <domain or ip> 80")?
- Can you use wget ("wget <url.")?
- Can you ping the host? ("ping <domain>")
- Are your DNS servers working well? (check /etc/resolv.conf)
Has anything changed in the firewall (transparent proxy or the like)?
ASKER
telnet host 80 works. I can ping the host. The firewall hasnt changed. Here's the code with the IP xx'd out
function get_rate3($currency) {
$ch = curl_init();
$url = 'http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy='.$currency;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT' ]);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error != '') {
echo $error;
}
if ($data != '') {
return $data;
} else {
return $error;
}
}
function get_rate3($currency) {
$ch = curl_init();
$url = 'http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy='.$currency;
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT'
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error != '') {
echo $error;
}
if ($data != '') {
return $data;
} else {
return $error;
}
}
Do you go through proxy.... ?
ASKER
No, thats not the problem. I created a clone of the server I am on and changed the IP of the URL to the clone server, and it works. However, when |I change back to the LIVE server it doesnt work. Dont forget that the LIVE and CLONE server are exact replicas created via amazons cloud AMI instance
boboodz,
Can you try to use curl command (on terminal, not in PHP) against the URL?
What curl version? Check this with curl -V.
Regards,
Fernando
Can you try to use curl command (on terminal, not in PHP) against the URL?
What curl version? Check this with curl -V.
Regards,
Fernando
What is the ACTUAL URL that you are having trouble with?
ASKER
curl -X GET "http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy=GBP" works perfectly.
Also, http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy=GBP works in any browser.
Finally, curl -V gives
curl 7.18.2 (i386-redhat-linux-gnu) libcurl/7.18.2 NSS/3.12.2.0 zlib/1.2.3 libidn/0.6.14 libssh2/0.18
Protocols: tftp ftp telnet dict http file https ftps scp sftp
Features: GSS-Negotiate IDN IPv6 Largefile SSL libz
Also, http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy=GBP works in any browser.
Finally, curl -V gives
curl 7.18.2 (i386-redhat-linux-gnu) libcurl/7.18.2 NSS/3.12.2.0 zlib/1.2.3 libidn/0.6.14 libssh2/0.18
Protocols: tftp ftp telnet dict http file https ftps scp sftp
Features: GSS-Negotiate IDN IPv6 Largefile SSL libz
ASKER
Please, try this function:
function get_rate3($currency)
{
$ch = curl_init('http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (($data = curl_exec($ch)) === false)
return curl_error($ch);
return $data;
}
function get_rate3($currency)
{
$ch = curl_init('http://x.x.x.x/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (($data = curl_exec($ch)) === false)
return curl_error($ch);
return $data;
}
ASKER
1) From within the server in a php script - it works
2) When I move it to a web server, then it freezes again
2) When I move it to a web server, then it freezes again
Well, I recommend you enable the PHP error log (display_errors = on) in php.ini. Something is wrong with server configuration or in another piece of code. This function works great here, with your URL.
Have you ran a PHP script in same server? This exclude libcurl, curl and PHP version.
Have you ran a PHP script in same server? This exclude libcurl, curl and PHP version.
So it works within the server (i.e. localhost), but not from the "web server"? When on the web server, is it trying to connect again to "localhost" or to another server? If another server, check the firewall on the "web server". It might be blocking either the outgoing port 80 or the replies from the other server.
ASKER
The cURL command runs fine by itself on the server.
Are you saying you have run a dummy script calling http://184.73.181.10/customer/getdextcountryrate.php?country=ALL¤cy=GBP via cURL from another server and it works?
Are you saying you have run a dummy script calling http://184.73.181.10/customer/getdextcountryrate.php?country=ALL¤cy=GBP via cURL from another server and it works?
I have put the follow code in a page called curl.php under Apache and accessed it from browser:
<?php
function get_rate3($currency)
{
$ch = curl_init('http://184.73.181.10/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
return $data;
}
$ret = get_rate3("GBP");
?>
Everything works fine. Are you using Apache? Hava you analyzed their logs too?
<?php
function get_rate3($currency)
{
$ch = curl_init('http://184.73.181.10/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
return $data;
}
$ret = get_rate3("GBP");
?>
Everything works fine. Are you using Apache? Hava you analyzed their logs too?
Run this. You might want to trim() the returned $htm string. Best regards, ~Ray
<?php // RAY_temp_curl_example.php
error_reporting(E_ALL);
function my_curl($url, $timeout=2, $error_report=FALSE)
{
$curl = curl_init();
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
$header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";
$header[] = "Pragma: "; // BROWSERS USUALLY LEAVE BLANK
// SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6');
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
// RUN THE CURL REQUEST AND GET THE RESULTS
$htm = curl_exec($curl);
// ON FAILURE HANDLE ERROR MESSAGE
if ($htm === FALSE)
{
if ($error_report)
{
$err = curl_errno($curl);
$inf = curl_getinfo($curl);
echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
var_dump($inf);
}
curl_close($curl);
return FALSE;
}
// ON SUCCESS RETURN XML / HTML STRING
curl_close($curl);
return $htm;
}
// USAGE EXAMPLE - PUT YOUR FAVORITE URL HERE
$url = "http://184.73.181.10/customer/getdextcountryrate.php?country=ALL¤cy=GBP";
$htm = my_curl($url);
if (!$htm) die("NO $url");
// SHOW WHAT WE GOT
echo "<pre>";
echo htmlentities($htm);
ASKER
Very frustrating!
function get_rate3($currency)
{
$ch = curl_init('http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
echo $data;
}
get_rate3("GBP");
?>
is the main server. It doesnt work.
Yet, 184.73.181.10 is a clone and it works from there!! This is turning out to be very tricky.
function get_rate3($currency)
{
$ch = curl_init('http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
echo $data;
}
get_rate3("GBP");
?>
is the main server. It doesnt work.
Yet, 184.73.181.10 is a clone and it works from there!! This is turning out to be very tricky.
Can you paste the new error here? Or is it the same?
And the logs? You need to check this.
And the logs? You need to check this.
ASKER
same error, the web page just freezes. If you run
function get_rate3($currency)
{
$ch = curl_init('http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
echo $data;
}
get_rate3("GBP");
?>
does it work for you?
function get_rate3($currency)
{
$ch = curl_init('http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
echo $data;
}
get_rate3("GBP");
?>
does it work for you?
Yes...
32=5.54 57=6.54 104=4.66 139=5.09 142=11.97 156=5.40 174=5.03 191=6.13 237=4.74 247=15.32 286=18.41 297=5.51 301=5.23 321=5.01 407=5.09 440=5.03 454=8.92 458=12.55 491=12.53 499=5.39 530=5.15 596=4.85 639=5.04
32=5.54 57=6.54 104=4.66 139=5.09 142=11.97 156=5.40 174=5.03 191=6.13 237=4.74 247=15.32 286=18.41 297=5.51 301=5.23 321=5.01 407=5.09 440=5.03 454=8.92 458=12.55 491=12.53 499=5.39 530=5.15 596=4.85 639=5.04
Did you check the firewall (telnet or wget) from the server that's failing?
ASKER
OK - it looks certain that the webserver is the issue.
So to narrow it down, can anyone work out why dextersim.com/xx.php doesnt work?
I have attached xx.php here
So to narrow it down, can anyone work out why dextersim.com/xx.php doesnt work?
I have attached xx.php here
<?php
function get_rate3($currency)
{
$ch = curl_init('http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy='.$currency);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
if (($data = curl_exec($ch)) === false)
return "cURL error: ".curl_error($ch);
echo $data;
}
get_rate3("GBP");
//echo "hello";
?>
The problem isn't the PHP code, but the server. So, you need to check the server, not the code. I have asked you if PHP have "display_errors=on" directive in php.ini and Apache is also logging transactions, but you don't answer me. =/
You need a troubleshooting, not a code inspection. ;)
You need a troubleshooting, not a code inspection. ;)
ASKER
I set display_errors = on at 204.236.220.231
There were no errors. I have reset it back to off.
The problem is that dextersim.com/xx.php cannot connect to 204.236.220.231. Every other server I have tried can connect to 204.236.220.231
For example
1) http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy=GBP will work from a browser
2) http://75.101.165.154/xx.php will work (xx.php is pointing to 204.236.220.231)
3) However, dextersim.com/xx.php does not work
The problem is clearly the setup of the server in (3) above. I simply cant find it yet.
There were no errors. I have reset it back to off.
The problem is that dextersim.com/xx.php cannot connect to 204.236.220.231. Every other server I have tried can connect to 204.236.220.231
For example
1) http://204.236.220.231/customer/getdextcountryrate.php?country=ALL¤cy=GBP will work from a browser
2) http://75.101.165.154/xx.php will work (xx.php is pointing to 204.236.220.231)
3) However, dextersim.com/xx.php does not work
The problem is clearly the setup of the server in (3) above. I simply cant find it yet.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.