fork download
  1. <?php
  2.  
  3. function getNSEStockQuote($symbol) {
  4. $cookieFile = __DIR__ . "/nse_cookie.txt";
  5.  
  6. // Step 1: Load NSE homepage to set cookies
  7. $ch = curl_init();
  8. curl_setopt($ch, CURLOPT_URL, "https://w...content-available-to-author-only...a.com/");
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  10. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
  11. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
  12. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
  13. curl_setopt($ch, CURLOPT_HEADER, true);
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  15. curl_exec($ch);
  16. curl_close($ch);
  17.  
  18. // Step 2: Call the actual quote API
  19. $apiUrl = "https://w...content-available-to-author-only...a.com/api/quote-equity?symbol=" . urlencode($symbol);
  20.  
  21. $headers = [
  22. "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64)",
  23. "Accept: application/json",
  24. "Referer: https://w...content-available-to-author-only...a.com/get-quotes/equity?symbol=" . urlencode($symbol),
  25. "Accept-Language: en-US,en;q=0.9",
  26. "Connection: keep-alive"
  27. ];
  28.  
  29. $ch = curl_init();
  30. curl_setopt($ch, CURLOPT_URL, $apiUrl);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  33. curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
  34. curl_setopt($ch, CURLOPT_ENCODING, "");
  35. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  36. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  37.  
  38. $response = curl_exec($ch);
  39. curl_close($ch);
  40.  
  41. $data = json_decode($response, true);
  42.  
  43. if (!$data || isset($data['error'])) {
  44. return "Failed to fetch stock data.";
  45. }
  46.  
  47. return [
  48. 'symbol' => $data['info']['symbol'] ?? '',
  49. 'companyName' => $data['info']['companyName'] ?? '',
  50. 'lastPrice' => $data['priceInfo']['lastPrice'] ?? '',
  51. 'dayHigh' => $data['priceInfo']['intraDayHighLow']['max'] ?? '',
  52. 'dayLow' => $data['priceInfo']['intraDayHighLow']['min'] ?? '',
  53. 'volume' => $data['marketDeptOrderBook']['tradeInfo']['totalTradedVolume'] ?? '',
  54. ];
  55. }
  56.  
  57. // Example
  58. $symbol = "TATAMOTORS"; // Try RELIANCE, SBIN, INFY etc.
  59. $result = getNSEStockQuote($symbol);
  60.  
  61. print_r($result);
  62.  
Success #stdin #stdout 0.03s 25912KB
stdin
Standard input is empty
stdout
Failed to fetch stock data.