如何通过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
- Wrong Content-Type Header: Your AJAX request sends form-urlencoded data, but you set
Content-Type: application/jsonin PHP. The Highcharts export server expects form data, not JSON payloads. - Incorrect Parameter Encoding: You urlencoded the entire parameter string, which broke how the server parses individual fields like
options. - Mishandling the Response: The export server returns a plain text string (the chart's relative path), not JSON—so
json_decodereturns 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_querysafely 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_VERIFYPEERin production (set totrue) 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
相关产品推荐
相关产品推荐

