AnyChart / export-server

Export Server is a Java based server intended for exporting charts in .pdf, .jpg, .png, .svg, .csv, .xslx, .json and .xml formats
http://export.anychart.com/status
Apache License 2.0
20 stars 6 forks source link

data-type must be one of the values: svg, script from php #2

Closed elteto closed 6 years ago

elteto commented 6 years ago

I receive the following error when executing the following code in php

{ "error": "data-type must be one of the values: svg, script" }

This is my code using the sample in (https://docs.anychart.com/Common_Settings/Server-side_Rendering)

when i run in console, with curl, its Ok. but from php give an error.

<?php
$params=[
    'responseType'=>'file', 
    'dataType'=>'script',
    'data'=>'var chart = anychart.line([1,2,5]); chart.container("container"); chart.draw();',
    ];

$headers = array('Content-Type: application/x-www-form-urlencoded');

$defaults = array(
        CURLOPT_URL=>'http://localhost:3000/jpg',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $params,
        CURLOPT_HTTPHEADER=> $headers
);

$ch = curl_init();
curl_setopt_array($ch, $defaults);
$picture = curl_exec($ch);
curl_close($ch);

$fh = fopen('filename.jpg', 'w');
fwrite($fh, $picture);
fclose($fh);
?>
Shestac92 commented 6 years ago

@elteto There are several things to correct in your php code.

  1. All items in $params should be represented as a single string.
  2. You should add CURLOPT_FILE to define the output for CURL.
  3. The export server requires another header: 'Accept: application/json'
  4. The current version of export-server has some issues with exporting to jpg. I'd like to suggest you use the dev-preview of upcoming version. You can download it from our CDN.

Below is your modified PHP code which was tested and works well:

<?php
$fh = fopen('filename.jpg', 'w+');

$data = "responseType=file&dataType=script&data=var chart = anychart.line([1,2,5]); chart.container('container'); chart.draw()";

$headers = array('Accept: application/json');

$defaults = array(
        CURLOPT_URL=>'http://localhost:3000/jpg',
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER=> $headers,
        CURLOPT_FILE=> $fh
);

$ch = curl_init();
curl_setopt_array($ch, $defaults);
$picture = curl_exec($ch);
curl_close($ch);

fwrite($fh, $picture);
fclose($fh);
?>
elteto commented 6 years ago

It works! Thanks!