2010-09-06 30 views
1

我遇到了freebase MQL登錄服務的問題。我正在發佈一個請求,然後freebase api應該發回頭文件,然後我將分析並獲取信息。cURL和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

'$ this'是什麼?你在一個物體裏面嗎?我不完全明白你在那裏做什麼(但是,我從來沒有使用HEADERFUNCTION,所以它可能是我)。手冊中提到'回調函數的名稱需要兩個參數',所以我不明白你爲什麼要指定一個數組? – 2010-09-06 17:40:00

+0

是的,所有的代碼都在一個類中,$ this這個數組的原因是它在這個類中調用了readHeader。沒有它,它將是一個未定義的函數。 – Lizard 2010-09-06 17:44:05

+0

啊,當然。所以'readHeader'確實在你上面引用的函數之外?我確定它是,但排除它是某種語言/結構怪癖而不是CURL問題的可能性。 – 2010-09-06 17:55:35

回答

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

希望這可以幫助別人!

+0

你應該接受你的答案 – 2010-09-21 00:06:27

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

}