开发者

CURL can't connect? Just hangs and doesn't respond. PHP has it enabled

I have a simple PHP function on a friend server which I've checked and has PHP CURL enabled.

The function is:

function sw_fetch_code($apikey='',$email=''){

 $url = "http://www.domain.com/xxx/api.php?getcode=1&apikey=".$apikey."&email=".$email."";


        $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_HEADER, 1); 
 $result = curl_exec($ch);

 curl_close($ch);

 $obj = json_decode($result);  

 if(!empty($obj)){
  if($obj->status == 200){

   return $obj->code;

  }else{
   return $obj->status;
  }
 }

}

As开发者_开发问答 you can see this is very simple and I've tested it and works on localhost and internal to my own server. The url returns as expected. However it just doesn't give any response when this function is called on my friends server.

Any ideas what could cause this?


First : check from the "friend" server if the URL works, as you donot have POST params, you can check with the exact query and get expected results. See if you can get the results on the browser on the friend server. If you don't have a GUI try wget on the command line. See if you get results. If you do go to next step, if you don't cURL isn't the problem. "friend server" isn't able to see your domain. Could be network issue / hosts etc .. (more on that if its the case)

Second: If you see results on step 1. Try this and see if you get anything:

$handle = fopen($url, "rb");
    $contents = '';
while (!feof($handle)) {
    $contents .= fread($handle,1024);
}

If you get response to this, then there is something wrong with cURL.


Does the curl_exec() call fail immediately, or does it hang for 30 seconds or so until it times out? If the latter, you may want to check for a firewall issue.

What does curl_getinfo($ch) tell you?


I think you should begin with standart checks:

If php is compiled with php_curl extension (or the extension is available as shared object). You can check it by putting a

<?php
if (!extension_loaded('curl')) 
{
    if (!dl('curl.so')) {
        die('Cannot load php_curl extension');
    }
}
?>

If the extension is loaded there might be a problem with dns/firewall on the friend's server. There also might be a requirement to use proxy server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜