Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 卷曲和自由基';s Api_Php_Curl_Freebase_Mql_Mqlwrite - Fatal编程技术网

Php 卷曲和自由基';s Api

Php 卷曲和自由基';s Api,php,curl,freebase,mql,mqlwrite,Php,Curl,Freebase,Mql,Mqlwrite,我在freebase MQL登录服务方面遇到问题。我正在发出一个post请求,然后freebase api应该发回标题,然后我将分析这些标题并从中获取信息 但我得到的唯一标题是HTTP/1.0 200 OK 代码 class myFreebaseClass { .... function doLogin() { echo $uri = "http://".$this->config['apiSandboxHost'].'/'.$this->config['apiLoginPat

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

}
我做错了什么?标题是否有误?


提前谢谢

这似乎是PHP的curl的一个bug,我在下面几行中也遇到了同样的问题:

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);
函数readHeader($ch,$string)
{
echo“Header:”.$string.“
”; } echo$uri=http://localhost/'; $ch=curl_init($uri); curl_setopt($ch,CURLOPT_头,1)//这一行也可以省略 curl_setopt($ch,CURLOPT_HEADERFUNCTION,'readHeader'); $output=curl\u exec($ch); 卷曲关闭($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;
    }

}
类myFreebaseClass{
....
函数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_头,1);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_HEADERFUNCTION,数组(&$this,'readHeader'));
$output=curl\u exec($ch);
//正在提取标题:
$infos=curl\u getinfo($ch);
$headers=substr($output,0,$infos['header_size']);
$headers=分解(“\n”,$headers);
//已完成提取标题
$output=substr($output,$infos['header_size']);
foreach($headers作为$header){
readHeader($ch,trim($header));
}
卷曲关闭($ch);
}
函数readHeader($ch,$string)
{
echo“Header:”.$string.“
”; if(strpos($string,'Set Cookie')!==false){ $this->authCookies[]=str_replace('Set-Cookie:','',$string); } 返回true; } }
结果是
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;
}

希望这对其他人有帮助

$this
是怎么回事?你在一个物体里面吗?我不完全理解你在那里做什么(但是,我从来没有使用过HEADERFUNCTION,所以可能是我)。手册中提到了回调函数的名称,其中回调函数采用两个参数,因此我真的不明白您为什么要指定数组?是的,所有代码都在一个类中,使用$this的数组的原因是它在这个类中调用readHeader。如果没有它,它将是一个未定义的函数。那么
readHeader
是否真的超出了您上面引用的函数范围?我肯定是的,但为了排除这是某种语言/结构上的怪癖而不是卷曲问题的可能性,我已经更新了代码,以显示它在类中。希望这更有意义
function readHeader($ch, $string)
{
    $length = strlen($string);
    if(strpos($string, 'Set-Cookie') !== false) {
        $this->authCookies[] = str_replace('Set-Cookie: ', '', $string);
    }
    return $length;
}