Introduction: API is Not Magic, It's a Tool
\nWhen your PHP site stops being a static page and starts communicating with the outside world — Telegram bots, payment gateways, or your own mobile app — you inevitably encounter APIs (Application Programming Interfaces). It's simply a contract: "If you send me a request in this format, I will return data to you." Today, on the PHP arena, there are three main players: classic REST, flexible GraphQL, and bold gRPC. Which one to choose? Let's figure it out with real-life examples.
\nThis article is written for those who already know how to write PHP and want to broaden their horizons. We won't just copy the documentation; we'll look at each protocol through the eyes of a developer solving a real problem.
\n\nREST API in PHP: The Tried-and-True Classic
\nREST (Representational State Transfer) is not a protocol, but an architectural style. It uses HTTP methods (GET, POST, PUT, DELETE) to work with resources. If you've ever written file_get_contents('https://api.example.com/users') — you've already worked with REST.
Why Does REST Remain King?
\n- \n
- Simplicity: any HTTP client can work with it. \n
- Caching: GET requests can be cached at the HTTP level. \n
- Broad Support: from curl to specialized libraries. \n
Example of a REST Client in PHP with Guzzle
\nThe Guzzle library is the de facto standard for HTTP requests in PHP. Install it via Composer: composer require guzzlehttp/guzzle.
<?php\nrequire 'vendor/autoload.php';\n\nuse GuzzleHttp\\Client;\n\n$client = new Client([\n 'base_uri' => 'https://jsonplaceholder.typicode.com/',\n 'timeout' => 5.0,\n]);\n\n// GET request\n$response = $client->get('posts/1');\n$post = json_decode($response->getBody(), true);\necho "Post title: " . $post['title'] . PHP_EOL;\n\n// POST request\n$response = $client->post('posts', [\n 'json' => [\n 'title' => 'New Post',\n 'body' => 'Post text',\n 'userId' => 1\n ]\n]);\necho "Created post with ID: " . json_decode($response->getBody(), true)['id'];\n?>\nWhat did we do? We simply sent HTTP requests. REST doesn't require a special library — you can even use file_get_contents() with a context, but Guzzle provides convenient error handling, middleware, and streaming.
When is REST Not the Best Choice?
\nIf you need to fetch related data (e.g., a post + author + comments), REST will force you to make multiple requests. This is where GraphQL steps in.
\n\nGraphQL in PHP: Request Only What You Need
\nGraphQL is a query language developed by Facebook. The client describes the response structure itself. One request — one response, without over-fetching or under-fetching data.
\nHow Does GraphQL Work in PHP?
\nServer-side: the webonyx/graphql-php library (installation: composer require webonyx/graphql-php). Client-side: a regular HTTP POST request with a JSON body.
Example of a GraphQL Query from PHP
\n<?php\nrequire 'vendor/autoload.php';\n\nuse GuzzleHttp\\Client;\n\n$client = new Client(['base_uri' => 'https://countries.trevorblades.com/']);\n\n$query = '\nquery GetCountry {\n country(code: "RU") {\n name\n capital\n currency\n languages {\n name\n }\n }\n}';\n\n$response = $client->post('', [\n 'json' => ['query' => $query]\n]);\n\n$data = json_decode($response->getBody(), true);\necho "Country: " . $data['data']['country']['name'] . PHP_EOL;\necho "Capital: " . $data['data']['country']['capital'] . PHP_EOL;\necho "Currency: " . $data['data']['country']['currency'] . PHP_EOL;\necho "Languages: ";\nforeach ($data['data']['country']['languages'] as $lang) {\n echo $lang['name'] . " ";\n}\n?>