PHP code example of vector88 / webutils

1. Go to this page and download the library: Download vector88/webutils 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/ */

    

vector88 / webutils example snippets




'providers' => [
    ...
    Vector88\WebUtils\Providers\WebUtilsServiceProvider::class,
    ...
],

'aliases' => [
    ...
    'XmlHelper'          => Vector88\WebUtils\Facades\XmlHelper::class,
	'HttpRequestHelper'  => Vector88\WebUtils\Facades\HttpRequestHelper::class,
    ...
],



ector88\WebUtils\XmlHelper;
use Vector88\WebUtils\HttpRequestHelper;



// Build a request (using Laravel with IOC)
$request = HttpRequestHelper::uri( $uri )
  ->authUsername( 'user' )
  ->authPassword( 'password' )
  ->method( 'POST' )
  ->header( 'Content-type', 'text/plain' )
  ->header( 'Cache-control', 'no-cache' )
  ->body( "Hello World! Here's some data..." );

// Execute the request
$response = $request->execute();

// If the request was successful:
if( $response->success ) {

  // Display the response code
  echo "{$response->code}<br />";

  // Display the response headers
  foreach( $response->headers as $header ) {
    echo "$header<br />";
  }

  // Display the response body
  echo "<br />";
  echo $response->body;

// Otherwise, if the request failed then display the error message and code
} else {
  echo "Request failed: {$response->errstr} ({$response->errno}).";

}



// Build a request (not using Laravel)
$request = new HttpRequestHelper();
$request->uri( $uri )
  ...


$xml = XmlHelper::loadStream( $xmlString );
...


$xml = new XmlHelper();
$xml->loadStream( $xmlString );
...



// Load an XML String (using Laravel with IOC)
$xml = XmlHelper::loadStream( $xmlString );



// Load a DOMDocument
$xml = XmlHelper::setElement( $document );



// Load a DOMNode
$xml = XmlHelper::setElement( $node );



// Create an empty XML object (with a document element)
$xml = XmlHelper::create();


$catalogue = $xml->findFirst( 'catalogue' );


$book = $xml->findFirst( '//book' );


$book = $xml->findFirst( '/catalogue/books/book' );
$book = $xml->findFirst( '//books/book[starts-with(@isbn, "97805")]' );
$book = $xml->findFirst( '/catalogue//book[contains(author, "kien")]' );
...


$books = $xml->findAll( '//books' );


$catalogueHelper = $xml->helper( 'catalogue' );


// GOOD. Do this.
$bookHelper = $xml->helper( 'catalogue/books/book' );

// OK. Because './' gets prepended, this is actually acceptable.
$bookHelper = $xml->helper( '/book' );

// BAD. Don't do this.
$bookHelper = $xml->helper( ''//book' );



$bookHelpers = $xml->helpers( 'catalogue/books/book' );


// Retrieve a helper for the first book element that can be found
$book = $xml->helper( '/book' );

// Retrieve and display the book title and year
echo "Book Title: " . $book->string( "title" ) . "<br />";
echo "Year: " . $book->integer( "year" ) . "<br />";


$title = $xml->string( '/book/title' );
$year = $xml->integer( '/book/year' );


$allBookTitles = $xml->strings( '/book/title' );
$allBookYears = $xml->integers( '/book/year' );


$book = $xml->helper( '/book' );
$isbn = $book->stringAttribute( 'isbn' );


$booksHelper = $xml->helper( '/catalogue/books' );

// Add (and get) a new book element to the books element
$newBookHelper = $booksHelper->add( 'book' );

// Use the XML helper to set some attributes and add additional children
$newBookHelper->setAttribute( 'isbn', '9780764555893' );
$newBookHelper->add( 'title', 'PHP and MySQL for Dummies' );
$newBookHelper->add( 'author', 'Valade, J' );
$newBookHelper->add( 'year', '2002' );


$xmlString = $xml->toString();


$xml = XmlHelper::loadStream( $xmlString );
$xml->withNamespace( "http://example.com/animals", "ns" );
$animals = $xml->findAll( "ns:animals/ns:animal" );


$animalHelpers = $xml->helpers( "ns:animals/ns:animal" );
foreach( $animalHelpers as $animalHelper ) {
  echo $animalHelper->string( "ns:species" ) . "<br />";
  echo $animalHelper->string( "ns:subspecies" ) . "<br />";
}