ds34222222 2017-04-12 12:49
浏览 96
已采纳

无法在php中使用curl获取json结果

Here I am try to get the JSON data from live share market(https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=tcs).

This url is return the JSON data in web browser. But I cannot get the JSON details using curl OR file_get_contents.

PHP Code:

$url='https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=tcs';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$result=curl_exec($ch);
curl_close($ch);    
$result_array =json_decode($result, true);

print_r($result_array);//Empty result
  • 写回答

1条回答 默认 最新

  • doulei6330 2017-04-12 12:56
    关注

    Try this one, it is working fine. You were missing some required headers. This url doesn't give any result without this header User-Agent

    <?php
    $url='https://www.nseindia.com/live_market/dynaContent/live_watch/get_quote/ajaxGetQuoteJSON.jsp?symbol=tcs';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,array(
    "Accept-Encoding:UTF-8",
    "Content-type: application/json",
    "User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"));
    $result=curl_exec($ch);
    curl_close($ch);
    
    $result_array =json_decode($result, true);
    
    print_r($result_array);
    

    Output:

    Array
    (
        [futLink] => /live_market/dynaContent/live_watch/get_quote/GetQuoteFO.jsp?underlying=TCS&instrument=FUTSTK&expiry=27APR2017&type=-&strike=-
        [otherSeries] => Array
            (
                [0] => EQ
            )
    
        [lastUpdateTime] => 12-APR-2017 16:00:00
        [tradedDate] => 12APR2017
        [data] => Array
            (
                [0] => Array
                    (
                        [extremeLossMargin] => 5.00
                        [cm_ffm] => 1,27,324.62
                        [bcStartDate] => -
                        [change] => -27.30
                        [buyQuantity3] => -
                        [sellPrice1] => 2,393.25
                        [buyQuantity4] => -
                        [sellPrice2] => -
                        [priceBand] => No Band
                        [buyQuantity1] => -
                        [deliveryQuantity] => 3,31,274
                        [buyQuantity2] => -
                        [sellPrice5] => -
                        [quantityTraded] => 5,25,269
                        [buyQuantity5] => -
                        [sellPrice3] => -
                        [sellPrice4] => -
                        [open] => 2,422.50
                        [low52] => 2,051.90
                        [securityVar] => 3.85
                        [marketType] => N
                        [pricebandupper] => 2,661.20
                        [totalTradedValue] => 12,624.32
                        [faceValue] => 1.00
                        [ndStartDate] => -
                        [previousClose] => 2,419.30
                        [symbol] => TCS
                        [varMargin] => 7.50
                        [lastPrice] => 2,392.00
                        [pChange] => -1.13
                        [adhocMargin] => -
                        [companyName] => Tata Consultancy Services Limited
                        [averagePrice] => 2,403.40
                        [secDate] => 12APR2017
                        [series] => EQ
                        [isinCode] => INE467B01029
                        [indexVar] => -
                        [pricebandlower] => 2,177.40
                        [totalBuyQuantity] => -
                        [high52] => 2,744.80
                        [purpose] => INTERIM DIVIDEND RS 6.50 PER SHARE
                        [cm_adj_low_dt] => 15-NOV-16
                        [closePrice] => 2,393.25
                        [isExDateFlag] => 
                        [recordDate] => 24-JAN-17
                        [cm_adj_high_dt] => 12-AUG-16
                        [totalSellQuantity] => 565
                        [dayHigh] => 2,428.00
                        [exDate] => 23-JAN-17
                        [sellQuantity5] => -
                        [bcEndDate] => -
                        [css_status_desc] => Listed
                        [ndEndDate] => -
                        [sellQuantity2] => -
                        [sellQuantity1] => 565
                        [buyPrice1] => -
                        [sellQuantity4] => -
                        [buyPrice2] => -
                        [sellQuantity3] => -
                        [applicableMargin] => 12.50
                        [buyPrice4] => -
                        [buyPrice3] => -
                        [buyPrice5] => -
                        [dayLow] => 2,383.00
                        [deliveryToTradedQuantity] => 63.07
                        [totalTradedVolume] => 5,25,269
                    )
    
            )
    
        [optLink] => /marketinfo/sym_map/symbolMapping.jsp?symbol=TCS&instrument=-&date=-&segmentLink=17&symbolCount=2
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验