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 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?