khalyomede/pulsar-php

API request and response, without using CURL.

v3.6.0 2018-06-20 09:48 UTC

This package is auto-updated.

Last update: 2024-04-15 09:09:52 UTC


README

API request and response, without using CURL.

Packagist PHP from Packagist Packagist

 

Pulsar-PHP logo

Summary

Installation

In your project, add the following dependency:

composer require khalyomede/pulsar-php:3.*

PHP support

To use this library for PHP 5.3+ until 5.6, use the version 1.* of this library. Note the version 1 and 2 are no longer maintainted.

Examples

Sending a GET request

require(__DIR__ . '/../vendor/autoload.php');

$content = pulsar()->get('https://jsonplaceholder.typicode.com/posts/1')->content();

print_r($content);
stdClass Object
(
  [userId] => 1
  [id] => 1
  [title] => sunt aut facere repellat provident occaecati excepturi optio reprehenderit
  [body] => quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto
)

Sending a POST request

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->data([
  'title' => 'Test your PHP libraries with Matcha',
  'userId' => 1,
  'body' => 'Lorem ipsum'
])->post('https://jsonplaceholder.typicode.com/posts');

print_r($response->content());
stdClass Object
(
  [title] => Test your PHP libraries with Matcha
  [userId] => 1
  [body] => Lorem ipsum
  [id] => 101
)

Sending a PATCH request

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->data([
  'name' => 'morpheus',
  'job' => 'zion resident'
])->patch('https://reqres.in/api/users/2');

print_r($response->content());
stdClass Object
(
  [name] => morpheus
  [job] => zion resident
  [updatedAt] => 2018-06-18T21:29:15.334Z
)

Sending a PUT request

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->data([
    'name' => 'neo',
    'job' => 'developer at Metacortex'
])->put('https://reqres.in/api/users/2');

print_r($response->content());
stdClass Object
(
    [name] => neo
    [job] => developer at Metacortex
    [updatedAt] => 2018-06-20T09:46:44.267Z
)

Sending a DELETE request

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->delete('https://reqres.in/api/users/2');

echo $response->code();
204

Sending a request to a non existing endpoint

In this case, you will always get a 404 status code and an empty response.

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->get('https://a-non-existing-domain-hopefully.com/api/v1/post');

echo $response->code();
404

Get the response as an array

You can do so by using ->toArray() modifier:

require(__DIR__ . '/../vendor/autoload.php');

Get the response as an array

You can use the toArray() modifier for this purpose:

require(__DIR__ . '/../vendor/autoload.php');

$array = pulsar()->get('https://jsonplaceholder.typicode.com/posts/1')->toArray()->content();

print_r($array);

Which is the same as:

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->get('https://jsonplaceholder.typicode.com/posts/1');

$array = $response->toArray()->content();

print_r($array);
Array
(
  [userId] => 1
  [id] => 1
  [title] => sunt aut facere repellat provident occaecati excepturi optio reprehenderit
  [body] => quia et suscipit
suscipit recusandae consequuntur expedita et cum
reprehenderit molestiae ut ut quas totam
nostrum rerum est autem sunt rem eveniet architecto
)

Get the HTTP status code

require(__DIR__ . '/../vendor/autoload.php');

$response = pulsar()->get('https://jsonplaceholder.typicode.com/posts/1');

echo $response->code();
200

Credits