GET请求返回数组var_dump显示NULL NULL,排查取值问题求助
json_decode return NULL twice in my PHP cURL function? My Code:
private $api_url= "www.myurl.com"; private $api_key = "111abjkbjkvsdf3879"; private function getpage($url, $redirect=FALSE) { $url = $this->purge_url($url); $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); /*Output the value of the array NULL NULL*/ $p_result = curl_exec($ch); $array = json_decode(trim($p_result), TRUE); var_dump($array); if ($redirect) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); if (!$page=curl_exec($ch)) { $this->add_error("Remote GET connection to $url failed: ".curl_error($ch)); curl_close($ch); return false; } curl_close($ch); $this->xmlsource = $page; return $page; }
My Issue:
When I run this code, var_dump($array) outputs NULL NULL. I'm trying to send a request with an API key, and I'm wondering if this NULL is the actual return value from the API, or if there's an error in how I'm fetching/processing the response.
Let's walk through what's happening here and fix this step by step:
First off—you're running curl_exec twice in the same function, which is a big red flag. The first call grabs $p_result, then you tweak some cURL options and run it again for $page. That's part of why you're seeing unexpected output, but let's break down the core issues:
1. You're not using your API key at all!
You defined $api_key but never included it in the request. Most APIs require authentication—either via a query parameter (like ?api_key=your-key), a request header (e.g., Authorization: Bearer your-key), or a POST field. Since you're making a GET request, you probably need to append it to your URL before initializing cURL:
$url = $this->purge_url($url) . '?api_key=' . urlencode($this->api_key);
Without this, the API is almost certainly returning an error (like 401 Unauthorized) which is probably an HTML page, not valid JSON—hence json_decode spits out NULL.
2. Your cURL flow is broken
You shouldn't run curl_exec twice on the same handle. Once you execute a request, the handle is in a completed state, and running it again can lead to unexpected (and empty/invalid) responses. You need to set all your cURL options first, then run curl_exec once.
3. Why json_decode returns NULL
json_decode returns NULL when:
- The input string is empty
- The input isn't valid JSON (syntax errors, wrong format)
- The JSON is valid but can't be converted to an array (though that would return an object instead of NULL if you don't pass
TRUEas the second param)
To debug this, add var_dump($p_result); right after $p_result = curl_exec($ch);—this will show you exactly what the API is sending back. It's probably an error message or empty content, not valid JSON.
Fixed Version of Your Function
Here's a revised version that fixes these issues and adds better debugging:
private function getpage($url, $redirect=FALSE) { $url = $this->purge_url($url); // Add API key to URL (adjust based on your API's requirements) $url .= '?api_key=' . urlencode($this->api_key); $ch = curl_init($url); // Set ALL cURL options first curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($redirect) { curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); $page = curl_exec($ch); // Check for cURL execution errors if (!$page) { $this->add_error("Remote GET connection to $url failed: ".curl_error($ch)); curl_close($ch); return false; } // Check HTTP status code (critical for debugging API errors) $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_code >= 400) { $this->add_error("API returned HTTP error $http_code: $page"); curl_close($ch); return false; } curl_close($ch); // Process JSON response $array = json_decode(trim($page), TRUE); // Check if JSON decoding failed if (json_last_error() !== JSON_ERROR_NONE) { $this->add_error("JSON decode failed: " . json_last_error_msg()); // Uncomment below to see raw response for debugging // var_dump($page); } var_dump($array); $this->xmlsource = $page; return $page; }
Key Debugging Tips
- Check the HTTP status code: Use
curl_getinfo($ch, CURLINFO_HTTP_CODE)to see if the API returned an error (4xx/5xx codes). - Inspect the raw response: Print out
$pageto see exactly what the API sends back—this is the fastest way to spot issues like error pages or malformed JSON. - Use
json_last_error(): This will tell you exactly whyjson_decodefailed (e.g., syntax error, empty input).
To Answer Your Original Question
That NULL is not the intended return value from the API. It's a result of:
- Missing the API key in your request, leading to an error response that can't be decoded as JSON.
- Running
curl_exectwice on the same handle, which causes the first request to return invalid/empty data. - The API returning non-JSON content (like an error page) due to authentication failure or incorrect request parameters.
内容的提问来源于stack exchange,提问作者antrez770

