代码之家  ›  专栏  ›  技术社区  ›  Lizard

cURL和Freebase的Api

  •  1
  • Lizard  · 技术社区  · 14 年前

    我在使用freebase MQL登录服务时遇到问题。我做了一个post请求,然后freebase api应该发回头文件,然后我将分析并从中获取信息。

    但我唯一得到的头球是 HTTP/1.0 200 OK

    代码

    class myFreebaseClass {
    
    ....
    
    function doLogin() {
    
    echo $uri = "http://".$this->config['apiSandboxHost'].'/'.$this->config['apiLoginPath'].'username='.$this->config['apiLoginUser'].'&password='.$this->config['apiLoginPass'];
    
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
    $output = curl_exec($ch);
    curl_close($ch);
    
    }
    
    function readHeader($ch, $string)
    {
        echo "Header: ".$string."<Br />";
        if(strpos($string, 'Set-Cookie') !== false) {
            $this->authCookies[] = str_replace('Set-Cookie: ', '', $string);
        }
        return true;
    }
    
    }
    

    输出

    http://sandbox.freebase.com/api/account/login?username=dXXXXX&password=XXXX
    Header: HTTP/1.0 200 OK 
    

    我做错什么了?我得到的标题不正确吗?

    2 回复  |  直到 14 年前
        1
  •  2
  •   Lizard    14 年前

    这最终成为了一个问题 readHeader() 功能。在我的例子中,我回来了 true . 当我返回每个标题的长度时,一切都正常了。例如

    function readHeader($ch, $string)
    {
        $length = strlen($string);
        if(strpos($string, 'Set-Cookie') !== false) {
            $this->authCookies[] = str_replace('Set-Cookie: ', '', $string);
        }
        return $length;
    }
    

    希望这能帮助别人!

        2
  •  0
  •   aularon    14 年前

    这似乎是PHP的curl的一个bug,我在下面几行中也遇到了同样的问题:

    function readHeader($ch, $string)
    {
        echo "Header: ".$string."<Br />";
    }
    
    echo $uri = 'http://localhost/';
    
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HEADER, 1);//this line can also be omitted
    curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'readHeader');
    $output = curl_exec($ch);
    curl_close($ch);
    

    你必须用传统的方法提取邮件头:

    class myFreebaseClass {
    
    ....
    
    function doLogin() {
    
        echo $uri = "http://".$this->config['apiSandboxHost'].'/'.$this->config['apiLoginPath'].'username='.$this->config['apiLoginUser'].'&password='.$this->config['apiLoginPass'];
    
        $ch = curl_init($uri);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
        $output = curl_exec($ch);
    
        //extracting headers:
        $infos = curl_getinfo($ch);
        $headers = substr($output, 0, $infos['header_size']);
        $headers = explode("\n", $headers);
        //done extracting headers
        $output = substr($output, $infos['header_size']);
    
        foreach($headers as $header) {
            readHeader($ch, trim($header));
        }
        curl_close($ch);
    
        }
    
        function readHeader($ch, $string)
        {
            echo "Header: ".$string."<Br />";
            if(strpos($string, 'Set-Cookie') !== false) {
                $this->authCookies[] = str_replace('Set-Cookie: ', '', $string);
            }
            return true;
        }
    
    }
    
    推荐文章