You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PHP获取Highcharts图片URL并解决返回NULL问题?

Fixing Highcharts Export in PHP: From Null Responses to Working Downloads

Let's break down why your PHP code was failing and walk through a corrected implementation that mirrors your working AJAX logic.

Key Issues in Your Original PHP Code

  1. Wrong Content-Type Header: Your AJAX request sends form-urlencoded data, but you set Content-Type: application/json in PHP. The Highcharts export server expects form data, not JSON payloads.
  2. Incorrect Parameter Encoding: You urlencoded the entire parameter string, which broke how the server parses individual fields like options.
  3. Mishandling the Response: The export server returns a plain text string (the chart's relative path), not JSON—so json_decode returns null.

Corrected PHP Implementation

This code will fetch the chart path, build the full image URL, and download the file to your server:

<?php
// Step 1: Define your chart options (matches your AJAX config)
$chartOptions = [
    'xAxis' => [
        'categories' => ['Ene', 'Feb', 'Mar', 'Abr']
    ],
    'series' => [
        ['data' => [1, 2, 3, 4], 'type' => 'line'],
        ['data' => [5, 6, 7, 8], 'type' => 'line']
    ]
];

// Step 2: Prepare POST parameters (properly structured)
$postParams = [
    'async' => 'true',
    'type' => 'image/jpeg',
    'width' => 800,
    'options' => json_encode($chartOptions)
];

// Step 3: Encode parameters as form-urlencoded (matches AJAX behavior)
$postData = http_build_query($postParams);

// Step 4: Configure cURL for the export request
$exportUrl = 'https://export.highcharts.com/';
$ch = curl_init($exportUrl);

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postData,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/x-www-form-urlencoded' // Critical fix!
    ],
    CURLOPT_SSL_VERIFYPEER => false // Disable only if your server lacks CA certs (not recommended for production)
]);

// Step 5: Execute the request and handle response
$response = curl_exec($ch);

if (curl_errno($ch)) {
    die('cURL Error: ' . curl_error($ch));
}
curl_close($ch);

// Step 6: Process the chart path and download the image
if (!empty($response)) {
    $chartFullUrl = $exportUrl . $response;
    echo "Generated Chart URL: " . $chartFullUrl . "<br>";

    // Save the image to your server
    $savePath = 'highcharts_chart.jpg';
    $imageData = file_get_contents($chartFullUrl);
    
    if ($imageData) {
        file_put_contents($savePath, $imageData);
        echo "Chart saved successfully to: " . $savePath;
    } else {
        echo "Failed to download chart image";
    }
} else {
    echo "No response from Highcharts export server";
}
?>

What This Fixes

  • Proper Content-Type: The header now matches what the export server expects (form-urlencoded data).
  • Correct Encoding: http_build_query safely encodes each parameter, including the JSON options string, so the server can parse it correctly.
  • Response Handling: We treat the server's output as plain text (the relative chart path) instead of trying to decode it as JSON.
  • Full Workflow: This code goes from generating the chart to saving it locally, perfect for cron jobs or server-side automation.

Alternative: Download Image with cURL (If file_get_contents is Disabled)

If your server blocks file_get_contents, replace the download section with this cURL-based approach:

// Alternative image download using cURL
$chImage = curl_init($chartFullUrl);
curl_setopt_array($chImage, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false
]);

$imageData = curl_exec($chImage);
if (curl_errno($chImage)) {
    die('cURL Error downloading image: ' . curl_error($chImage));
}
curl_close($chImage);

file_put_contents($savePath, $imageData);

Production Notes

  • Re-enable CURLOPT_SSL_VERIFYPEER in production (set to true) to ensure secure SSL connections. You may need to configure your server's CA certificate path if you get SSL errors.
  • Ensure your PHP process has write permissions to the directory where you're saving the image.

内容的提问来源于stack exchange,提问作者Martin Rafael Pérez Lara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:23:04