PHP code example of sirmonti / simplehttp

1. Go to this page and download the library: Download sirmonti/simplehttp library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sirmonti / simplehttp example snippets


  $http=new simpleHTTP(2,false);

  $http=new simpleHTTP;
  $resp=$http->get('https://www.example.com/');
  printf("Response data:\n%s\n",$resp);

  $http=new simpleHTTP;
  $http->get('https://www.example.com/');
  $resp=$http->PSRResponse();

  $http=new simpleHTTP;
  $resp1=$http->get('https://www.example.com/data1.txt');
  $resp2=$http->get('https://www.example.com/data2.json');

  // Exception level 2 (exception on network or HTTP errors)
  // Certificate validation disabled (Will accept any certificate)
  $http=new simpleHTTP(2,false);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  // Execute a POST request sending the content of the $data variable
  $resp=$http->post('https://www.example.com/sendpoint',$data);

  // Default exception level is 1, which means exceptions will be fired
  // only on network errors
  $http=new simpleHTTP;
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  $resp=$http->postJSON('https://www.example.com/jsonentrypoint',$data);

  $http=new simpleHTTP;
  $data=file_get_contents('exampleimage.jpg');
  $http->postRAW('https://www.example.com/imagenentrypoint','image/jpeg',$data);

  $http=new simpleHTTP;
  // Create the authentication header
  $headers=[
    'Authorization: Bearer AuthenticationToken'
  ];
  // Configure the default headers to send
  $http->setExtraHeaders($headers);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  $resp=$http->post('https://www.example.com/sendpoint',$data);

  $http=new simpleHTTP;
  $headers=[
    'Authorization: Bearer AuthenticationToken'
  ];
  $http->setExtraHeaders($headers);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  // Add an extra header to this request
  $resp=$http->post('https://www.example.com/sendpoint',$data,['X-Extra-Header: Data']);

  $http=new simpleHTTP;
  // Add an User-Agent header
  $headers=[
    'Authorization: Bearer AuthenticationToken',
    'User-Agent: MyOwnUserAgent/1.0'
  ];
  $http->setExtraHeaders($headers);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  $resp=$http->post('https://www.example.com/sendpoint',$data);

  $http=new simpleHTTP;
  $headers=[
    'Authorization: Bearer AuthenticationToken',
    'User-Agent: MyOwnUserAgent/1.0'
  ];
  $http->setExtraHeaders($headers);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  // Example PUT with body in JSON format
  $resp=$http->putJSON('https://www.example.com/sendpoint',$data);

  $http=new simpleHTTP;
  $resp=$http->get('https://www.example.com/');
  printf("HTTP/%s %d %s\n",$http->protocolVersion(),$http->respCode(),$http->respStatus());
  printf("Response mime type: %s\n",$http->respMIME());