This might help: https://stackoverflow.com/questions/7911535/how-to-unit-test-curl-call-in-php
Another solution is using Guzzle, which you can easily mock!
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I'm using laravel frameworks to commit cURL requests and process its data. I'm able to do these things fine, but I want to build test cases for them too.
Here is an example of a cURL request function I might use:
public function getLatestCourse() {
$token = $this->accessToken;
$headers = ['Authorization: Bearer ' . $token];
$curl = curl_init();
$url = "https://my.instructure.com/api/v1/accounts/1/courses?sort=created_at&order=desc&per_page=30";
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => TRUE,
CURLINFO_HEADER_OUT => TRUE,
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_SSL_VERIFYPEER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER, TRUE
]);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl) . "<br>";
} else { echo " all good<br>"; }
$resp = curl_exec($curl);
$data = json_decode($resp, true);
curl_close($curl);
return $data[0];
}
My question, to be clear, is if given a request and an action like this, what kinds of unit test cases could I make for them? I feel like I'd have to restructure the code to test each individual part, but I'm not sure how
Any help would be greatly appreciated
Please or to participate in this conversation.