douzhongju8780 2018-05-17 17:20
浏览 74
已采纳

从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 2018-05-17 17:55
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗