0

I have a class

class exchange_rates{

    var $currency = '"USDINR","TRYINR","SARINR","EURINR"'; 
    var $url ;
    var $handle;
    var $result;
    var $xml_index , $xml_val;

    public function __construct(){

            $this->url = 'http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ('.$this->currency.')&env=store://datatables.org/alltableswithkeys';


    }

    public function fetch_Currency (){ 

            //echo $this->url ;
            $this->handle = file_get_contents($this->url);

            if ($this->handle) {
                    $this->result = fgets($this->handle, 4096);
                    $p = xml_parser_create();

                    xml_parse_into_struct($p, $this->result, $this->xml_val, $this->xml_index);

                    xml_parser_free($p);

                    fclose($this->handle);
        }
    }

    public function get_exchange(){
        echo "<pre>";
        var_dump($this->xml_val);
        echo "</pre>";
    }

}

I am calling the class like this

$obj = new exchange_rates();
    $obj->fetch_Currency();
    $obj->get_exchange();

The error I am getting is as follow

Warning: file_get_contents(http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDINR","TRYINR","SARINR","EURINR")&env=store://datatables.org/alltableswithkeys): failed to open stream: HTTP request failed! HTTP/1.0 400 Unknown Version in C:\xampp\htdocs\ycc\currency_convertor.php on line 21
NULL

Basically I am trying to get the XML from the URL . If you parse the following url

 http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.xchange where pair in ("USDINR","TRYINR","SARINR","EURINR")&env=store://datatables.org/alltableswithkeys

You will see the XML result , But Through my code I am not able to download

Please help me out

Thanks

1 Answer 1

4

Just came across this question when I was debugging the same thing. The solution for me was you have to urlencode the query part of the URL.

$query = 'select * from yahoo.finance.xchange where pair in ("USDINR","TRYINR","SARINR","EURINR")';
$request = 'http://query.yahooapis.com/v1/public/yql?q=' . urlencode($query) . '&env=store://datatables.org/alltableswithkeys';

$data = file_get_contents($request);

Hope this helps someone!

More info: https://developer.yahoo.com/php/howto-reqRestPhp.html

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.