cURL is a commonly used and powerful tool when using PHP for HTTP requests. Many developers use cURL to complete the request when calling APIs or crawling web page data. However, if the returned data is not correctly captured before calling curl_close() , it may lead to data loss or error handling. Therefore, this article will introduce in detail how to correctly capture and process the data returned by cURL requests before calling curl_close() .
The basic steps to using cURL are as follows:
Initialize the cURL session;
Set options;
Execute the request;
Get the return value;
Close the session.
For example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
In this example, $response saves the content returned by the remote server. Note that getting the return value must be done before the curl_close() call , because curl_close() will free the resource and the response data will no longer be accessed.
To ensure that the data is captured intact and reliably, we should pay attention to the following points:
This is the most critical step. By default, cURL outputs the result directly to standard output (browser), rather than returning the value. If you want to further process the return content through the code, you must set this option:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
After executing the request, it is recommended to judge the return value:
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
die("cURL Request failed: $error");
}
This prevents unresponsive or erroneous responses from being mistakenly processed as valid data.
If you need to determine whether the request is successful, such as whether the HTTP status code is 200, you can also use:
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Use in conjunction with judgment logic:
if ($httpCode !== 200) {
curl_close($ch);
die("Request failed,HTTP Status code: $httpCode");
}
Here is a complete example showing how to safely capture and process returned data:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://gitbox.net/api/userinfo");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // Optional:Set timeout
$response = curl_exec($ch);
if ($response === false) {
$error = curl_error($ch);
curl_close($ch);
die("Request error: $error");
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
die("接口Request failed,Status code: $httpCode");
}
// deal with JSON response
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("JSON Analysis failed: " . json_last_error_msg());
}
// Sample output
echo "username: " . $data['username'];
Before closing a cURL session with curl_close() , be sure to:
Use CURLOPT_RETURNTRANSFER to get the returned data;
Check whether curl_exec() succeeds;
Optionally obtain the status code to determine whether the request is successful;
Finally, curl_close() is called to release the resource.
Through the above steps, you can ensure that every cURL request is handled securely and securely. This is especially critical for building robust network communication capabilities.
Related Tags:
cURL