douzhongju8780
douzhongju8780
2018-05-17 17:20

从PHP获取highcharts图像URL

已采纳

I need to get a highcharts image, and I found a code to resolve my problem, obtaining the name of the image via ajax, and then I paste before the url of the Highcharts API, and I can download the image in PHP, now, I want to do this process in PHP, since the name, until the download; I have this code to get the name of highcharts image:

        var optionsStr = JSON.stringify({
  "xAxis": {
    "categories": [
      "PRUEBA",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec"
    ]
  },
  "series": [
    {
      "data": [1, 3, 2, 4],
      "type": "line"
    },
    {
      "data": [5, 3, 4, 2],
      "type": "line"
    }
  ]
}),
        exportUrl ='https://export.highcharts.com/';
        dataString = encodeURI('async=true&type=image/jpeg&width=800&options=' + optionsStr);
            $.ajax(
            {
                type: 'POST',
                data: dataString,
                url: exportUrl,
                success: function(data)
                {
                    console.log('get the file from relative url: ', data);
                    grafica=exportUrl+data;
                },
                error: function(err)
                {
                    console.log('error', err.statusText)
                }
            });

I get the name of the graphic: charts/chart.9973ffbcf0d748d6aae04ef6ec01979c.jpeg I want to know how get the same in PHP, I have this code:

$DataEncoded = json_encode(
    array(
    'xAxis'=>
        array(
            'categories'=> ['Ene','Feb', 'Mar', 'Abr']
        ),
    'series'=>
        array(
            array(
                'data'=>[1,2,3,4],
                'type'=>'line'
            ),
            array(
                'data'=>[5,6,7,8],
                'type'=>'line'
            )
        )
    )
);
$DataExtra = 'async=true&type=image/jpeg&width=800&options=';
$dataString = urlencode('async=true&type=image/jpeg&width=800&options='.$DataEncoded);
$url ='https://export.highcharts.com/';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
$json=curl_exec($ch);
if(curl_errno($ch)){
    echo "Entro al curl Error";
    throw new Exception(curl_error($ch));
}
var_dump(json_decode($json));
curl_close($ch);

But I receive a fantastic: NULL. Someone made this before? I tried making this process via Angular 5, but, I must to program a cron job to delegate the process, so, I decided to use PHP. I tried with file_get_contents() but the webservice give me: "No data chart found".

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douni9620 douni9620 3年前

    I suspect your issue is here:

    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
    

    I believe this is expecting a String or an Array.

    This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile.

    Consider trying the following:

    $dataArray = array(
      "async" => true,
      "type" => "image/jpeg",
      "width" => 800,
      "options" => $DataEncoded
    );
    

    And...

    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataArray);
    

    Credit to @Patrick Q for catching the Encoding Type. You're not exactly sending it JSON encoded data, but Form data essentially.

    点赞 评论 复制链接分享
  • drkbpwk609294 drkbpwk609294 3年前

    I used the recommendations of Twisty and finally I obtain the name of the image, thank you so much.

    $dataString = ('async=true&type=image/jpeg&width=800&options='.$DataEncoded);
    $url ='https://export.highcharts.com/?';
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$dataString);
    curl_setopt($ch, CURLOPT_POST, 1);
    
    $headers = array();
    $headers[] = "Accept: application/json";
    $headers[] = "Content-Type: application/x-www-form-urlencoded";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $result = curl_exec($ch);
    
    点赞 评论 复制链接分享