GET requests are the baseline PHP cURL call for reading an API resource without sending a request body. Build the URL with its query string, keep the response in PHP with CURLOPT_RETURNTRANSFER, and check the HTTP status separately from transport errors before using the body.
curl_exec() reports whether the cURL transfer itself ran. An HTTP error response such as 404 or 500 is still a completed transfer, so use curl_getinfo() to read the response code after the request finishes.
A local PHP built-in server on 127.0.0.1 gives the request a disposable endpoint before the same pattern is pointed at a real API. Use an https:// API URL for production requests, keep certificate verification enabled, and add authentication headers only when the target endpoint requires them.
Related: How to read response headers with PHP cURL
Related: How to handle HTTP errors with PHP cURL
Related: How to decode a JSON response with PHP cURL
Related: How to set a timeout in PHP cURL
Steps to send a GET request with PHP cURL:
- Confirm that the PHP cURL extension is loaded in the runtime that will run the script.
$ php -r 'var_export(extension_loaded("curl")); echo PHP_EOL;' trueInstall or enable the platform cURL extension package, such as php-curl on Debian or Ubuntu systems, when this command prints false.
- Create a local endpoint that returns the request method and query value as JSON.
- weather-api.php
<?php header('Content-Type: application/json'); $city = $_GET['city'] ?? 'unknown'; echo json_encode([ 'method' => $_SERVER['REQUEST_METHOD'], 'city' => $city, 'forecast' => 'rain expected', ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL;
- Start the local endpoint in a second terminal while testing the client script.
$ php -S 127.0.0.1:8080 weather-api.php
Stop the built-in server with Ctrl+C after the request test is complete.
- Create the PHP cURL GET client script.
- get-weather.php
<?php $city = $argv[1] ?? 'Kuala Lumpur'; $query = http_build_query(['city' => $city]); $url = 'http://127.0.0.1:8080/weather?' . $query; $curl = curl_init($url); if ($curl === false) { fwrite(STDERR, 'Could not initialize cURL' . PHP_EOL); exit(1); } curl_setopt_array($curl, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Accept: application/json', ], CURLOPT_TIMEOUT => 10, ]); $response = curl_exec($curl); if ($response === false) { fwrite(STDERR, 'cURL error: ' . curl_error($curl) . PHP_EOL); exit(1); } $httpCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); echo "HTTP status: {$httpCode}" . PHP_EOL; echo $response; if ($httpCode < 200 || $httpCode >= 300) { exit(1); }
http_build_query() encodes the query string before it is appended to the URL. A new cURL handle sends a GET request when the URL is set and no request-body options such as CURLOPT_POST or CURLOPT_POSTFIELDS are enabled.
- Run the client script and confirm the endpoint received a GET request.
$ php get-weather.php HTTP status: 200 { "method": "GET", "city": "Kuala Lumpur", "forecast": "rain expected" }A 2xx status plus the expected response body shows that PHP captured the response instead of printing it before the status check.
- Replace the local URL with the target API endpoint after the local request works.
Do not put secrets in a GET query string. Query strings can appear in browser history, web server logs, proxy logs, monitoring tools, and copied support transcripts.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.