Call a REST API in PHP

Call a REST API in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Making PHP Talk to REST APIs Without the Guesswork

When you are handed a REST API by a client and the documentation is sparse, the immediate instinct is often to treat it like a black box. You need to know which HTTP method to use, what headers are required for authentication, and how to structure the payload. Unfortunately, legacy tutorials found on search engines often point to outdated resources, such as expired Yahoo! guides that focus on SOAP protocols or ignore modern header requirements entirely. Even educational sites sometimes default to describing SOAP methods while leaving REST implementation vague.

To bridge this gap, you must rely on the standard HTTP specification rather than guessing based on incomplete examples. A REST API fundamentally operates over HTTP, meaning your PHP code needs to construct a request that mimics a browser or another client application. The most direct approach in vanilla PHP involves using the cURL library, which has been a staple for server-side HTTP interactions since PHP 5.5.

Here is a concrete example of how to initiate a GET request when the API documentation is thin:

$ch = curl_init('https://api.example.com/resource');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Returns response instead of outputting directly
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Accept: application/json',
    'Authorization: Bearer YOUR_TOKEN'
]);
$response = curl_exec($ch);
if ($response === false) {
    die('cURL error: ' . curl_error($ch));
}
echo $response;

The critical decision here lies in configuration. Setting CURLOPT_RETURNTRANSFER to true ensures the response is captured as a string rather than being sent directly to the output buffer. This allows you to parse the JSON data using json_decode() before processing it. Furthermore, headers are often where the missing information hides. If the API requires authentication via a Bearer token or specific content types, these must be explicitly defined in the header array. Without them, the server will likely return a 401 Unauthorized or 400 Bad Request error, which is a common pitfall for developers new to REST consumption.

If you are working within the Laravel ecosystem, the HttpClient facade offers a more modern and concise alternative that abstracts away the cURL boilerplate. This is particularly useful if your application relies heavily on framework conventions. You can define an HTTP client with default headers once and reuse it across your controllers:

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Accept' => 'application/json',
    'Authorization' => 'Bearer ' . config('app.api_token')
])->get('https://api.example.com/resource');

if ($response->successful()) {
    $data = $response->json();
    // Process data
} else {
    throw new \Exception("API request failed: " . $response->status());
}

This approach leverages Laravel's built-in JSON response handling, making error checking and data parsing significantly cleaner than manual string manipulation. The trade-off is a dependency on the framework, but given that many PHP applications today use Composer-managed packages, this overhead is negligible compared to the clarity gained.

When documentation is truly absent, you must be prepared to inspect network traffic manually. Use your browser's Developer Tools or tools like Postman to see how a native request is structured, then replicate those headers and parameters in your PHP code. Relying on vague online tutorials that omit header details is risky; always verify the Content-Type and authentication mechanisms required by the target service. By adhering to standard HTTP verbs (GET for retrieval, POST for creation) and ensuring proper header negotiation, you can successfully integrate external services regardless of how sparse their official guides may be.

Our network