PHP Creating a CGI request and getting response

The following code shows the use of the PEAR HTTP_Request package/library (see http://pear.php.net/package/HTTP_Request2 and https://pear.php.net/manual/en/package.http.http-request2.intro.php for introduction on how to use it) to create a CGI request and process the response. This package must be installed on the PHP server (is not default). Documenation of HTTP

>>> NOTE - PEAR and HTTP_Request2 has been installed on our csweb01 server

 

Example 1 - A simple get request

<?php
require_once 'HTTP/Request2.php';

$request = new HTTP_Request2('http://pear.php.net/'HTTP_Request2::METHOD_GET);
try {
    
$response $request->send(); //SENDING request
    if (
200 == $response->getStatus()) {//GETTING RESPONSE
        echo 
$response->getBody();
    } else {
        echo 
'Unexpected HTTP status: ' $response->getStatus() . ' ' .
             
$response->getReasonPhrase();
    }
} catch (
HTTP_Request2_Exception $e) {
    echo 
'Error: ' $e->getMessage();
}
?>

RESPONSE

 

 

 

Example 2 - Get request and setting request parameters

<?php

require_once 'HTTP/Request2.php';


// Explicitly set request method and use_brackets
$request = new HTTP_Request2('http://pear.php.net/bugs/search.php',
HTTP_Request2::METHOD_GET, array('use_brackets' => true));
$url $request->getUrl();

//set up the Get parameter variables
$url->setQueryVariables(array(
'package_name' => array('HTTP_Request2''Net_URL2'),
'status'       => 'Open'
));
$url->setQueryVariable('cmd''display');

// This will output a page with open bugs for Net_URL2 and HTTP_Request2
echo $request->send()->getBody();
?>

 

 

 

 

Example setting Get Request HEADER parameters

<?php

require_once 'HTTP/Request2.php';

$request = new HTTP_Request2('https://csweb01.csueastbay.edu/~grewe/CS3520/PHP/ProcessGetHeader.php'HTTP_Request2::METHOD_GET);


// setting one header
$request->setHeader('Accept-Charset''utf-25');

// setting several headers in one go
$request->setHeader(array(
'Connection' => 'close',
'Referer'    => 'http://localhost/'
));

// removing a header
$request->setHeader('User-Agent'null);

//NOW Continue on with code including eventually a $request->send() and recieving the $response

$request->send()->getBody();
?>

IF your server like csweb01 program is not setup with certificates properly --use this version

 

<?php
require_once 'HTTP/Request2.php';
             
$request = new HTTP_Request2('https://csweb01.csueastbay.edu/~grewe/CS3520/PHP/ProcessGetHeaders.php', HTTP_Request2::METHOD_GET);
             
// setting one header
 $request->setHeader('Accept-Charset', 'utf-25');
// setting several headers in one go
$request->setHeader(array(
               'Connection' => 'close',
               'Referer'    => 'http://localhost/'
               ));
// removing a header
$request->setHeader('User-Agent', null);
// ######### To Fix the SSL issue ###########
 $request->setConfig(array(
               'ssl_verify_peer'   => FALSE,
               'ssl_verify_host'   => FALSE
               ));
// ########################################
try {
               $response = $request->send();
               if (200 == $response->getStatus()) {
               echo $response->getBody();
               } else {
               echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
               $response->getReasonPhrase();
               }
 } catch (HTTP_Request2_Exception $e) {
               echo 'Error: ' . $e->getMessage();
}
?>

RESPONSE

..first here is the ProcessGetHeader.php

<?php
             foreach (getallheaders() as $name => $value) {
             echo "$name: $value\n";
             }
?>

 

 

Example - authentication

<?php
// This will set credentials for basic auth
$request = new HTTP_Request2('http://user:password@www.example.com/secret/');

// This will set credentials for Digest auth
$request->setAuth('user''password'HTTP_Request2::AUTH_DIGEST);
?>

 

 

 

Example - Adding cookies to the request

$request->addCookie('CUSTOMER''WILE_E_COYOTE');
$request->addCookie('PART_NUMBER''ROCKET_LAUNCHER_0001');

 

 

Example - A POST request setting parameters AND uploading files of data to be sent as body

 

<?php
$request 
= new HTTP_Request2('http://www.example.com/profile.php');
$request->setMethod(HTTP_Request2::METHOD_POST)
->
addPostParameter('username''vassily')
->
addPostParameter(array(
'email' => 'vassily.pupkin@mail.ru',
'phone' => '+7 (495) 123-45-67'
))
->
addUpload('avatar''me_and_my_cat.jpg''image/jpeg');


//next send request and get response
?>

 

Special NOTE:

1) above shows adding single parameter 'username;

2) above shows adding an array of parameters (email,phone)

3) above shows how to send files of data

addUpload() can accept either a string with a local file name or a pointer to an open file, as returned by fopen(). You currently can't directly pass a string with the file contents, however you can pass a pointer to php://memory or php://temp:

//an example passing filenames and a file pointer $fp
//create file pointer loaded with data from a file

$fp fopen('php://temp/maxmemory:1048576''r+');
fwrite($fpgenerateFileUploadData());


$request->addUpload('stuff'$fp'custom.name''application/octet-stream');

 

4) File uploads will be streamed from disk by HTTP_Request2_MultipartBody to reduce memory consumption.

 

SPECIAL note ---if the server like our csweb01 server program does not handle the SSL communications for https request you need to alter the code above with ######

 

<?php
               require_once 'HTTP/Request2.php';
$request = new HTTP_Request2('https://csweb01.csueastbay.edu/~grewe/CS3520/PHP/ProcessPost.php');
             
$request->setMethod(HTTP_Request2::METHOD_POST)
               ->addPostParameter('username', 'vassily')
               ->addPostParameter(array(
               'email' => 'vassily.pupkin@mail.ru',
               'phone' => '+7 (495) 123-45-67'
               ));
// ######### To Fix the SSL issue ###########
$request->setConfig(array(
               'ssl_verify_peer'   => FALSE,
               'ssl_verify_host'   => FALSE
               ));
 // ########################################
//invoke request and get the response
 try {
               $response = $request->send();
               if (200 == $response->getStatus()) {
               echo $response->getBody();
               } else {
               echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
               $response->getReasonPhrase();
               }
} catch (HTTP_Request2_Exception $e) {
               echo 'Error: ' . $e->getMessage();
 }
?>

Here is my ProcessPost.php and the results

<?php
$email = $_POST['email'];
                 $username = $_POST['username'];
                 $phone = $_POST['phone'];
echo 'username: ' . $email;
                 echo ' & email: ' . $username;
                 echo ' & phone: ' . $phone;
?>

 

© Lynne Grewe