1

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!

Lizard
  • 43,732
  • 39
  • 106
  • 167
  • What's with the `$this`? Are you inside an object? I don't entirely understand what you're doing there (but then, I have never worked with HEADERFUNCTION so it could be me). The manual speaks of `The name of a callback function where the callback function takes two parameters` so I don't really understand why you are specifying an array? – Pekka Sep 06 '10 at 17:40
  • Yeah all code is within a class, the reason for the array with $this is so that it calls readHeader within this class. Without it it would be an undefined function. – Lizard Sep 06 '10 at 17:44
  • ah, of course. So `readHeader` is really outside the function you quote above? I'm sure it is, but to exclude the possibility that it's some language/structural quirk rather than a CURL issue. – Pekka Sep 06 '10 at 17:55
  • I have updated my code to show that it is in a class. Hope that makes more sense – Lizard Sep 06 '10 at 18:06

2 Answers2

2

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!

Lizard
  • 43,732
  • 39
  • 106
  • 167
0

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

}
aularon
  • 11,042
  • 3
  • 36
  • 41