0

UPDATE: The problem is solved now. The problem was caused because of my university's proxy servers. Due to them I could not able to get data from yahoo api to my localhost. But When I upload the website on server, everything worked fine.


I am trying to get stock price from yahoo finance API, but when I enter stock symbol and this below warning appears. Please have a look at this code and point out my mistake. Thanks!

The image of warning page is Here

My code is

    $symbol=$_POST['symbol'];
    $url="http://download.finance.yahoo.com/d/quotes.csv?s=".trim($symbol)."&f=nsl1op&e=.csv";
    $handle=fopen($url, "r");
    if (!$handle) {
            $error = 'Not a valid URL.';
            return false;
        }
    $result=array();
    while ($row = fgetcsv($handle)){
        if (isset($row[1])) {
        array_push($result, array('company'=>$handle[0],'symbol'=>$handle[1],'last_trade'=>$handle[2], 'open'=>$handle['3'],'close'=>$handle['4']));
    }
    }
    fclose($handle);
    $quote_data=$result;

   if(!$quote_data){
        echo "<div class='alert alert-danger'>invaild symbol</div>";
        render("master",array('title'=>'FSE', 'view'=>'quote')); //will show the form again to enter symbol
        exit();
    }
    $title='Quotes for'.htmlspecialchars($quote_data['symbol']);
    render("master",array('view'=>'quote', 'title'=>$title, 'data'=>$quote_data)); //will show the page showing prices & symbols of company
Community
  • 1
  • 1
Ashvini Kumar
  • 107
  • 1
  • 2
  • 7
  • That means the destination URL at `download.finance.yahoo.com` is not accepting connections from your client. Are you able to open the same `$url` in a browser successfully from the _same_ machine? Some hosts deny connections to certain user agent strings while permitting others, or this one could just be firewalling you out. – Michael Berkowski Oct 04 '14 at 20:34
  • For what it's worth, I _can_ open that URL from a browser, and I can also call it from PHP with `file_get_contents()`. – Michael Berkowski Oct 04 '14 at 20:36
  • yes, when I put "http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=nsl1op&e=.csv"{lets assume $symbol=goog} then I am able to download CSV file which contain data of google. – Ashvini Kumar Oct 04 '14 at 20:38
  • And does it block you if you just attempt to `echo file_get_contents('http://download.finance.yahoo.com/d/quotes.csv?s=goog&f=nsl1op&e=.csv');` ? – Michael Berkowski Oct 04 '14 at 20:40
  • I used "file_get_contents()" but same problem continues. – Ashvini Kumar Oct 04 '14 at 20:40
  • excuse my ignorance, but i don't want to echo it but get its data and push in a array – Ashvini Kumar Oct 04 '14 at 20:41
  • @AshviniKumar: I cannot reproduce your problem with PHP 5.4, the code looks ok. But maybe you need to [escape parts of your URL](http://stackoverflow.com/questions/8801280/how-to-escape-url-for-fopen)? – lxg Oct 04 '14 at 20:42
  • The `echo` is only for quick diagnostics... Try [the method described here](http://stackoverflow.com/a/3032658/541091) for setting a different user agent string. Use a user agent string similar to that of Firefox or IE, as some sites deny access for automated script user agents (though an API should not deny) – Michael Berkowski Oct 04 '14 at 20:42
  • @MichaelBerkowski Could you please explain that what can be the possible reason for target machine refusing the connection – Ashvini Kumar Oct 04 '14 at 20:47
  • @AshviniKumar There are many possible reasons. Perhaps a daily API limit was exceeded, or the service is only available to a geographically specific area where others are denied by IP. – Michael Berkowski Oct 04 '14 at 20:50
  • Thanks @MichaelBerkowski taking your time out. I think it may be second reason because I am in India and trying to accessing API for Google price. Thanks once again. – Ashvini Kumar Oct 04 '14 at 20:53
  • But you were able to access it via the browser per your earlier comment, so it may not be geographically related. Try out the user agent modification I suggested. – Michael Berkowski Oct 04 '14 at 20:56
  • same problem continues even after changing user agent – Ashvini Kumar Oct 04 '14 at 21:01

1 Answers1

0


you have encoded a special char that you should not encoded it

http://download.finance.yahoo.com/d/quotes.csv?s=goog&amp;&f=nsl1op&e=.csv

the char & used to seperate parameters in URL should not be encoded in HTML -> &amp ;

let me know if was helped you

Halayem Anis
  • 7,654
  • 2
  • 25
  • 45