2017-09-14 98 views
0

我有以下設置:使用php導出Serverside上的Highcharts?

第一個PHP文件:從MySQL數據庫獲取數據,將其放入highchart,出口額爲「export.highcharts.com」爲PNG和帖子的圖片的URL到PHP文件2號通過阿賈克斯職位。

第二PHP文件:從PHP文件編號爲1的Ajax Post獲取「imagelink」,然後連接到twitter api以發佈包含該圖片的推文。

現在,只要我在瀏覽器中執行第一個PHP文件,這一切都很好。

我需要通過cronjob在「服務器端」運行這個自動化。現在它不再工作了,因爲它包含ajax和ajax文章,它不會在服務器端執行。

首先PHP文件的代碼:

<!-- Bootstrap core JavaScript --> 
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
     <!-- Highcharts --> 
    <script src="https://code.highcharts.com/highcharts.js"></script> 
    <script src="https://code.highcharts.com/highcharts-3d.js"></script> 
    <script src="https://code.highcharts.com/modules/exporting.js"></script> 



<?php 

    $server = "localhost"; 
    $username = "test"; 
    $password = "test"; 
    $db = "test"; 

    $connect = @mysql_connect($server, $username, $password); 

?> 


<?php if ($connect && mysql_select_db($db, $connect)) : ?> <?php 



     // Select twitterdata table 
     $printMarketdata = mysql_query("SELECT * FROM marketdata ORDER BY volume24h DESC LIMIT 10") or die("MySQL Error! ".mysql_error()); 

     // Marketdata found 
     if (mysql_num_rows($printMarketdata) > 0) { 


     ?> 


<script> 
// Create the chart 
var options = { 
    chart: { 
     type: 'bar' 
    }, 
    title: { 
     text: 'TOP 10 Highest Trading Volume (24h)' 
    }, 
    subtitle: { 
     text: 'Trading Volume (24h)' 
    }, 
    xAxis: { 
     type: 'category' 
    }, 
    yAxis: { 
     title: { 
      text: 'Volume' 
     } 

    }, 
    legend: { 
     enabled: false 
    }, 
    plotOptions: { 
     series: { 
      cursor: 'pointer', 
      borderWidth: 0, 
      dataLabels: { 
       enabled: true, 
       format: '${point.y:,.0f}' 
      }, 
     point: { 
       events: { 
        click: function() { 
         location.href = 'https://example.com/' + 
          this.options.key; 
        } 
       } 
      } 
     } 
    }, 

    tooltip: { 
     headerFormat: '<span style="font-size:11px">{series.name}</span><br>', 
     pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>${point.y:,.0f}</b><br/>' 
    }, 

    series: [{ 
     name: 'Trading Volume (24h)', 
     colorByPoint: true, 
     data: [ 

      <?php // output data of each row 
      while ($marketdata = mysql_fetch_array($printMarketdata)) 
      { 
       ?> 

     { 
      name: '<?php echo $marketdata['name'];?>', 
      y: <?php echo $marketdata['volume24h'];?>, 
      key: '<?php echo $marketdata['symbol'];?>' 
     }, 

      <?php 
      } 
      ?> 


     ] 
    }], 
       navigation: { 
     buttonOptions: { 
      enabled: false 
     } 
    }, 
    credits: { 
     enabled: false 
    } 

}; 



// URL to Highcharts export server 
    var exportUrl = 'https://export.highcharts.com/'; 

    // POST parameter for Highcharts export server 
    var object = { 
     options: JSON.stringify(options), 
     type: 'image/png', 
     async: true 
    }; 

    // Ajax request 
    $.ajax({ 
     type: 'post', 
     url: exportUrl, 
     data: object, 
     success: function (data) { 
      //Submit data from your server 
      // Ajax request 
      $.ajax({ 
       type: 'post', 
       url: 'posttweet.php',//this your local file 
       data: {'url' : exportUrl+data}, 
       success: function (data2) { 
        //Response from your server 
        //if your teste.php print response. echo "" or die("") ; 
        alert(data2); 
       } 
      }); 
     } 
    }); 


</script> 

<?php 





     // No Data found 
     } else { 
      echo "0 results"; 
     } 

    ?> 

<?php else : ?> 

<h2>Database connection failed!</h2> 

<?php endif; ?> 

難道你們有什麼好的代碼,我可以使用(最好是PHP),這樣我就可以讓highchart和出口作爲服務器端的圖像?

+0

要使用高圖服務器端,您將需要在您的服務器上一個完整的瀏覽器... PHP不會這樣做,它只會用來調用命令行和東西... – Salketer

+0

是的,那麼該怎麼辦這是最好的方式? – user8607695

回答

0

您無法從PHP運行AJAX,因此您需要用一些PHP替換您的JavaScript代碼。

您可以使用捲曲,或喜歡狂飲捲曲庫將請求發送到其他頁面,API等

看一看PHP的文檔的信息上捲曲: http://php.net/manual/en/curl.examples-basic.php

大約說,像這樣的:

第一個PHP文件:從MySQL數據庫獲取數據,將其放入highchart,出口額爲「export.highcharts.com」爲PNG 並要求文件2並將結果

第二個PHP文件:從其他PHP文件獲取「imagelink」,然後連接到twitter api,將包含圖像的推文發送到圖表。

+0

謝謝。嗯,所以我需要讓整個高調卷曲?還是隻有導出部分? – user8607695

+0

我真的不明白你的意思...是否有任何例子如何使用curl導出圖表作爲圖像?謝謝! :) – user8607695

+0

您可以使用選項生成圖表,向高圖導出服務器發送請求。它將返回具有指定格式的圖像。你可以在頁面底部的API描述 - https://www.highcharts.com/docs/export-module/export-module-overview – morganfree