1

我在使用 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 

我究竟做错了什么?我是否错误地获取标题?

提前致谢!

4

2 回答 2

2

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

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

希望这对其他人有帮助!

于 2010-09-07T07:55:43.550 回答
0

这似乎是 PHP 的 curl 的一个错误,我能够通过以下几行得到同样的问题:

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;
    }

}
于 2010-09-06T18:55:39.557 回答