cURL 和 Freebase 的 Api

发布于 2024-09-17 21:23:59 字数 1153 浏览 10 评论 0原文

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

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

Code

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

}

Outputs

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

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

提前致谢!

I am having problems with the freebase MQL login service. I am making a post request then the freebase api should send back headers which I will then analyse and get information from.

But the only header I am getting is HTTP/1.0 200 OK

Code

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

}

Outputs

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

What am I doing wrong? Am I getting the headers incorrectly?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒解孤独 2024-09-24 21:23:59

它最终是 readHeader() 函数的问题。在我的示例中,我返回了 true。当我返回每个标头的长度时,一切都起作用了。例如

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

希望这对其他人有帮助!

It ended up being an issue with the readHeader() function. In my example I was returning true. It all worked when I returned the length of each header. e.g.

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

Hope this helps someone else!

沦落红尘 2024-09-24 21:23:59

这似乎是 PHP 卷曲的一个错误,我在以下几行中遇到了同样的问题:

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

}

It seems to be a bug with PHP's curl, I was able to get the same problem with the following lines:

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

You have to do headers extraction the traditional way:

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文