【问题标题】:YQL : Getting unsupported http protocol errorYQL:获取不受支持的 http 协议错误
【发布时间】:2014-04-26 13:58:53
【问题描述】:

当我尝试通过 cURL 调用 YQL 时,出现以下错误。

不支持 HTTP 版本 说明:网络服务器“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;

?>

从浏览器调用相同的 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

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

【问题讨论】:

    标签: php http curl yql http-protocols


    【解决方案1】:

    问题可能是因为您提供给 cURL 的 url 无效。您需要准备/编码查询字符串的各个值以在 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 不包含您不能在 url 中使用的字符,但通常您会为任何您不知道或无法控制的值执行此操作.

    【讨论】:

    • 我还是看不到任何回应:(
    • @Anil 如果您使用http 而不是https 是否有效,并且错误消息是否相同?
    • @Anil 如果您真的需要,您可以为 https 设置/取消设置一些附加选项:ca.php.net/manual/en/function.curl-setopt.php
    【解决方案2】:

    好的,我必须得 .. 问题出在 https 上。使用以下 sn-p 进行调试

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

    ?>

    【讨论】:

      猜你喜欢
      • 2018-05-12
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多