代码之家  ›  专栏  ›  技术社区  ›  Lizard

freebase cURL和mqlwrite与PHP

  •  2
  • Lizard  · 技术社区  · 14 年前

    echo $uri = "http://".$this->config['apiSandboxHost'].'/'.$this->config['apiWritePath'].'={"create":"unless_exists","type":"/user/docs/music/note","name":"A","id":null}';
    
    $ch = curl_init($uri);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $headers = array();
    $headers[] = 'X-Metaweb-Request : idio';
    $headers[] = 'Content-length: 78';
    $headers[] = 'Content-type: application/x-www-form-urlencoded';
    $headers[] = 'Cookie: '.  implode(';', $this->authCookies);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $output = curl_exec($ch);
    pr(curl_getinfo($ch));
    curl_close($ch);
    echo $output;
    

    输出

    http://sandbox.freebase.com/api/service/mqlwrite?query={"create":"unless_exists","type":"/user/docs/music/note","name":"A","id":null}
    
    Array
    (
        [url] => http://sandbox.freebase.com/api/service/mqlwrite?query={"create":"unless_exists","type":"/user/docs/music/note","name":"A","id":null}
        [content_type] => 
        [http_code] => 0
        [header_size] => 0
        [request_size] => 693
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 285.371
        [namelookup_time] => 0
        [connect_time] => 0.185
        [pretransfer_time] => 0.185
        [size_upload] => 0
        [size_download] => 0
        [speed_download] => 0
        [speed_upload] => 0
        [download_content_length] => 0
        [upload_content_length] => -1
        [starttransfer_time] => 285.371
        [redirect_time] => 0
    )
    

    提前感谢您的帮助!

    1 回复  |  直到 14 年前
        1
  •  1
  •   Lizard    14 年前

    结果我把数据发送到freebase的方式出了问题。一旦我加上

    curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
    

    这很奇怪,因为只有当我将数据作为post变量发送并将内容添加到请求的查询字符串时,我才能使它工作。

    推荐文章