PHP code example of sirmonti / shttp

1. Go to this page and download the library: Download sirmonti/shttp 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 / shttp example snippets


  SHTTP::get('https://www.example.com/');
  printf("Response data:\n%s\n",$resp);

  // Disable certificate validation
  SHTTP::verifCERT=false;
  // Set exception level to 2
  SHTTP::setExceptionLevel(2);
  // All subsequent requests will operate with this parameters
  $resp=SHTTP::get('https://www.examples.com/');

  SHTTP::get('https://www.example.com/');
  $resp=SHTTP::PSRResponse();

  $resp1=SHTTP::get('https://www.example.com/data1.txt');
  $resp2=SHTTP::get('https://www.example.com/data2.json');

  // Exception level 2 (exception on network or HTTP errors)
  SHTTP::setExceptionLeve(2);
  // Certificate validation disabled (Will accept any certificate)
  SHTTP::verifCERT=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=SHTTP::post('https://www.example.com/sendpoint',$data);

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

  $data=file_get_contents('exampleimage.jpg');
  SHTTP::postRAW('https://www.example.com/imagenentrypoint','image/jpeg',$data);

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

  $headers=[
    'Authorization: Bearer AuthenticationToken'
  ];
  // This header will be used by all subsequent requests
  SHTTP::setExtraHeaders($headers);
  $data=[
    'field1'=>'Data to send in field1',
    'field2'=>'Data to send in field2'
  ];
  // Add an extra header to this request
  $resp=SHTTP::post('https://www.example.com/sendpoint',$data,['X-Extra-Header: Data']);

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

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

  $resp=SHTTP::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());