PHP Classes

Peter PHP HTTP Client Library: Access sites with HTTP request/response classes

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-12-16 (10 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 51 All time: 10,677 This week: 59Up
Version License PHP version Categories
peter-http 1.0MIT/X Consortium ...7HTTP, PHP 7
Description 

Author

This package can access sites with HTTP request and response classes.

It provides a HTTP request class that can use the PHP Curl extension to send HTTP requests to remote sites using given Curl HTTP request options.

The request class returns a response class object that applications can use to access the response to the HTTP returned by the HTTP server.

Picture of Chun-Sheng, Li
  Performance   Level  
Name: Chun-Sheng, Li <contact>
Classes: 37 packages by
Country: Taiwan Taiwan
Age: 31
All time rank: 21046 in Taiwan Taiwan
Week rank: 4 Up1 in Taiwan Taiwan Up
Innovation award
Innovation award
Nominee: 17x

Winner: 1x

Example

<?php

/*
* This the main PHP is to present the HTTP Request/Response example.
*/

// show the runtime error
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once
__DIR__.'/src/autoloader.php';

use
peter\Http\HttpRequest;
use
peter\Http\HttpResponse;

// read the api-key.ini to get the required API keys.
if(!file_exists('./api-key.ini')) {
    die(
'You need to create the api-key.ini file!'.PHP_EOL);
}

$apiKey = parse_ini_file('./api-key.ini', true);
$accessToken = $apiKey['Facebook']['access_token'];
$rebrandlyKey = $apiKey['Rebrandly']['api_key'];

$pageId = '167874227121383';
$formatString = 'https://graph.facebook.com/%s/feed?fields=%s&access_token=%s';
$fileldLists = [
   
'full_picture',
   
'created_time',
   
'message',
   
'likes',
   
'object_id',
];
$filelds = implode($fileldLists, ',');

// HTTP GET example (HTTP GET Facebook page id)
$requestUrl = sprintf($formatString, $pageId, $filelds, $accessToken);

// This is the default CURL options and you can override the options as you want.
$options = [
   
CURLOPT_RETURNTRANSFER => true, // return web page
   
CURLOPT_HEADER => false, // don't return headers
   
CURLOPT_FOLLOWLOCATION => true, // follow redirects
   
CURLOPT_ENCODING => '', // handle all encodings
   
CURLOPT_USERAGENT => 'Mozilla', // the user-agent
   
CURLOPT_AUTOREFERER => true, // set referer on redirect
   
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
   
CURLOPT_TIMEOUT => 120, // timeout on response
   
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
   
CURLOPT_SSL_VERIFYHOST => 0, // do verify ssl
   
CURLOPT_SSL_VERIFYPEER => true, //
   
CURLOPT_VERBOSE => 1, //
   
CURLOPT_COOKIEJAR => '', // the default cookie file path
   
CURLOPT_HTTPHEADER => [], // set the HTTP headers
];

$request = new HttpRequest($requestUrl, 'GET', $options);
$responseList = $request->httpRequest();

$response = new HttpResponse($responseList);
$result = json_decode($response->httpResponse(), true);
var_dump($result);

// HTTP POST example (POST the url to the shorten url via rebrandly service)
/*
$requestUrl = 'https://api.rebrandly.com/v1/links';
$domainData['fullName'] = 'rebrand.ly';
$postData['destination'] = 'https://www.messenger.com';
$postData['domain'] = $domainData;
$request->setHttpMethod('POST');
$request->setRequestUrl($requestUrl);
$args = [
    'headers' => [
        'apikey: '.$rebrandlyKey,
        'Content-Type: application/json',
    ],
    'body' => json_encode($postData),
];
$request->setArgs($args);
$responseList = $request->httpRequest();
$response = new HttpResponse($responseList);
$result = json_decode($response->httpResponse(), true);
var_dump($result);
*/


Details

wp-http

Introduction

The object-oriented style helper class helps you accessing the HTTP request/response.

Usage

  • 
    
  • Please see the more details about the ```main.php```.

Sample code

  • Create the ```api-key.ini``` in this project root path and the ini file contents are as follows:
[Facebook]
access_token=define_your_access_token
[Rebrandly]
api_key=define_your_api_key

  • We use the Facebook graph API and Rebrandly API to complete the ```GET``` and ```POST``` examples.
  • Please see the ```main.php``` to get the more details about ```GET``` and ```POST``` examples.

References

Here is the references about the WordPress HTTP API. - wp_remote_get - wp_remote_post - wp_remote_request

And in our helper class, we use the `wp_remote_get` and `wp_remote_post` to complete this project.

How to solve the curl(60):SSL certificate problem

TODO

Include the more available CURL options.


  Files folder image Files (16)  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file main.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (16)  /  src  
File Role Description
Files folder imagepeter (1 directory)
  Accessible without login Plain text file autoloader.php Aux. Auxiliary script

  Files folder image Files (16)  /  src  /  peter  
File Role Description
Files folder imageHttp (12 files)

  Files folder image Files (16)  /  src  /  peter  /  Http  
File Role Description
  Plain text file CurlOptions.php Class Class source
  Plain text file HtmlResponse.php Class Class source
  Plain text file HttpGetRequest.php Class Class source
  Plain text file HttpPostRequest.php Class Class source
  Plain text file HttpRequest.php Class Class source
  Plain text file HttpRequestInterface.php Class Class source
  Plain text file HttpResponse.php Class Class source
  Plain text file HttpResponseInterface.php Class Class source
  Plain text file JsonResponse.php Class Class source
  Plain text file ResponseFormatter.php Class Class source
  Plain text file TextResponse.php Class Class source
  Plain text file XmlResponse.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:51
This week:0
All time:10,677
This week:59Up