dsj1961061 2011-02-02 20:38
浏览 11
已采纳

无论如何要'间谍'curl_init()和curl_exec()的活动?

I have some PHP code that was at one point WORKING FINE. It makes a call out to an external API, the API has NOT CHANGED AT ALL. The PHP code has also NOT CHANGED AT ALL. But suddenly, I am getting no results back for this function:

if (!function_exists(setFieldsAndCallURL))
{ 
    function setFieldsAndCallURL($url,$fields)
    {

        //url-ify the data for the POST
        $fields_string='';
              foreach($fields as $key=>$value) 
                { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string,'&');

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        //execute the jump
        $result = '';
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);

        return $result;
      }
}

Before, it would return a text GUID when called:

$userURL ='https://api.nottherealendpointurl.net/public/user/authenticate';

$userFields = array(
     'username'=>$username,
     'lastName'=>$lastname,
     'firstName'=>$firstname,
     'email'=>$email,
     'token'=>urlencode($adminKey),
);

//Login this particular user
$userKey = setFieldsAndCallURL($userURL,$userFields);

But suddenly it has started returning "" (empty string) and I have no idea why.

Is there any way to get more info and spy on the inner workings of this thing? See the call it is making using HTTP header logging software? Or anything?

NOTE: I have already tested the POST manually to the API and it is working as expected, I am still getting back the proper GUID. For some reason doing it through this curl thing just suddenly quit doing it properly. Nobody has any idea what could be different now.

  • 写回答

2条回答 默认 最新

  • dounan9070 2011-02-02 20:55
    关注

    Propably the ip of the server is blocked by now, where your local ip is not?

    You might want to add

     $headerFile = fopen(filepath_to_header_file);
     $errorFile = fopen(filepath_to_error_file);    
    
     curl_setopt($ch, CURLOPT_VERBOSE, 1); 
     curl_setopt($ch, CURLOPT_HEADER, 1); 
     curl_setopt($ch, CURLOPT_WRITEHEADER, $headerFile ); 
     curl_setopt($ch, CURLOPT_STDERR, $errorFile );
    

    to get the header of the response and the errors into files and look their content up.

    edit: To verify if the ip of the server is blocked you could try something like this

    $host = "ssl://api.nottherealendpointurl.net/";
    $port = 443;
    $url = "/public/user/authenticate";
    
    $timeout = 30;
    $errno = "";
    $errstr= "";
    
    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
    if($fp)
    {
        $request = "GET ".$url." HTTP/1.1
    ";
        $request.= "Host: ".$host."
    ";
        $request.= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.7.12) Gecko/20050919 Firefox/1.0.7
    ";
        $request.= "Connection: Close
    
    ";
    
        fwrite($fp, $request);
        while (!feof($fp))
        {
            $data .= fgets($fp, 128);
        }
        fclose($fp);
        echo $data;
    }
    else
    {
        echo "ERROR: ".$errstr;
    }
    

    where $data contains the response from the remote server-

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集