YQL:获得不受支持的http协议错误

时间:2014-04-26 13:58:53

标签: php http curl yql http-protocols

当我试图通过cURL调用YQL时,我收到以下错误。

不支持HTTP版本 描述:Web服务器" engine1.yql.vip.bf1.yahoo.com"正在使用不受支持的HTTP协议版本。

以下是使用的代码     

    // URL
    $URL = "https://query.yahooapis.com/v1/public/yql?q=select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"&format=json";

    // set url
    curl_setopt($ch, CURLOPT_URL, $URL);

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $output = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

    echo $output;

>

从thr浏览器调用相同的URL工作正常

  

https://query.yahooapis.com/v1/public/yql?q=select *来自html其中   URL =" HTTP://www.infibeam.com/Books/search Q = 9788179917558"和   xpath =" // span [@class =' infiPrice金额价格'] / text()"& format = json

有人可以指出代码中有什么问题吗?

2 个答案:

答案 0 :(得分:1)

问题可能是因为您输入cURL的网址无效。您需要准备/编码查询字符串的各个值,以便在URL中使用。

您可以使用urlencode()执行此操作:

$q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"");

$URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";

在这种情况下,我只编码q的值,因为format不包含您不能在网址中使用的字符,但通常您会为任何值而执行此操作知道或控制。

答案 1 :(得分:1)

好的我gottacha ..问题在于https。使用以下代码段进行调试

if (false === ($data = curl_exec($ch))) {
        die("Eek! Curl error! " . curl_error($ch));
    }

默认情况下,添加以下代码以接受SSL证书。

$options = array(CURLOPT_URL => $URL,
        CURLOPT_HEADER => "Content-Type:text/xml",
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_RETURNTRANSFER => TRUE

    );

完整代码在这里

<?php
    // create curl resource
    $ch = curl_init();

    // URL
    $q = urlencode("select * from html where url=\"http://www.infibeam.com/Books/search?q=9788179917558\" and xpath=\"//span[@class='infiPrice amount price']/text()\"");
    $URL = "https://query.yahooapis.com/v1/public/yql?q={$q}&format=json";

    echo "URL is ".$URL;
    $ch = curl_init();

    //Define curl options in an array
    $options = array(CURLOPT_URL => $URL,
        CURLOPT_HEADER => "Content-Type:text/xml",
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_RETURNTRANSFER => TRUE

    );

    //Set options against curl object
    curl_setopt_array($ch, $options);

    //Assign execution of curl object to a variable
    $data = curl_exec($ch);
    echo($data);

    //Pass results to the SimpleXMLElement function
    //$xml = new SimpleXMLElement($data);

    echo($data);

    if (false === ($data = curl_exec($ch))) {
        die("Eek! Curl error! " . curl_error($ch));
    }

    if (200 !== (int)curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
        die("Oh dear, no 200 OK?!");
    }

    //Close curl object
            curl_close($ch);

&GT;