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?

Anil
  • 89
  • 4

2 Answers2

1

The problem is probably caused because the url you feed to cURL is not valid. You need to prepare / encode the individual values of the query strings for use in a url.

You can do that using 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";

In this case I have only encoded the value of q as the format does not contain characters that you cannot use in a url, but normally you'd do that for any value you don't know or control.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • Still I could see no response :( – Anil Apr 26 '14 at 14:24
  • @Anil Does it work if you use `http` instead of `https` and is the error message the same? – jeroen Apr 26 '14 at 14:31
  • @Anil There are some additional options you can set / unset for https if you really need it: http://ca.php.net/manual/en/function.curl-setopt.php – jeroen Apr 26 '14 at 14:40
1

Okay I gottacha .. The problem was with the https. Used the following snippet for debug

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

Added below code to accept SSL certificates by default.

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

    );

Complete code is here

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

?>

Anil
  • 89
  • 4