10. Web Services
Activity 10.1: Making Your Own POST Request to httpbin.org
Solution
- Create a
httpbin.php
file in theguzzle-example
directory. Require the Composer autoload file and import theGuzzle Client
class:<?php require 'vendor/autoload.php'; use GuzzleHttp\Client;
- Instantiate a new
Guzzle Client
by passing thehttpbin
address:$client = new Client(['base_uri'=>'http://httpbin.org/']);
- Inside a
try
…catch
block, make aPOST
request to the/response-headers
endpoint. Add anAccept
header set toapplication/json
and set two query parameter key-value pairs, withfirst
asJohn
andlast
asDoe
:try { $response=$client->request('POST', '/response-headers',[ 'headers'=>[ 'Accept'=>'application-json' ...