1

When i'm trying to invoke the YQL via cURL i'm getting the following error.

HTTP Version Not Supported Description: The web server "engine1.yql.vip.bf1.yahoo.com" is using an unsupported version of the HTTP protocol.

Following is the code used

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

?>

Invoking the same URL from thr browser works fine

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

Can someone please point me what is wrong in the code?

4

2 回答 2

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

在这种情况下,我只将 的值编码qformat不包含您不能在 url 中使用的字符,但通常您会为您不知道或无法控制的任何值执行此操作。

于 2014-04-26T14:10:06.060 回答
1

好的,我得了 .. 问题出在 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);

?>

于 2014-04-26T14:41:54.630 回答