我遇到了freebase MQL登录服务的问题。我正在发帖子请求然后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
我做错了什么?我的标题错误了吗?
提前致谢!
答案 0 :(得分:2)
最终成为readHeader()
函数的问题。在我的例子中,我正在返回true
。当我返回每个标题的长度时,这一切都有效。 e.g。
function readHeader($ch, $string)
{
$length = strlen($string);
if(strpos($string, 'Set-Cookie') !== false) {
$this->authCookies[] = str_replace('Set-Cookie: ', '', $string);
}
return $length;
}
希望这有助于其他人!
答案 1 :(得分:0)
这似乎是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;
}
}