Category Archives: PHP

stream_context_create

Stream_context_create — Creates a stream context.

Creates and returns a stream context with any options supplied in options preset.

<?php
 $opts = array(
 'http'=>array(
 'method'=>"GET",
 'header'=>"Accept-language: en\r\n" .
 "Cookie: foo=bar\r\n"
 )
 );

$context = stream_context_create($opts);
 $fp = fopen('http://www.phpcodez.com', 'r', false, $context);
 fpassthru($fp);
 fclose($fp);
?>

Arrays

• Way of ordering data by associating values to keys
• Unique keys are associated with a single value, or set of values
• Arrays can be nested, so that a value in one array actually represents a complete other array (multi-dimensional arrays)

Creating Arrays

INDEXED NUMERICALLY (INDEXED ARRAY)

o EX: $x = array(‘a’, ‘b’, ‘c’);
o EX: $x = [‘a’, ‘b’, ‘c’];
o EX: $x = array(0 => ‘a’, 1 => ‘b’, 2 => ‘c’);
o EX: $x = [0 => ‘a’, 1 => ‘b’, 2 => ‘c’];

INDEXED WITH STRINGS (ASSOCIATIVE ARRAY)

$x = array(
‘XML’ => ‘eXtensible Markup Language’
);
$x = [
‘XML’ => ‘eXtensible Markup Language’
];

Filling Arrays

• range() CREATES AN ARRAY WITH VALUES FROM AN INTERVAL

o DEFAULT STEP IS “1”

o EX: $x = range(1.2, 4.1) // == array(1.2, 2.2, 3.2)

Web Services

A web service is a software system designed for interoperable interaction over a network.

A web service is defined with a WSDL (Web Services Description Language) document, and other systems interact with the web service using SOAP messages, transferred using HTTP with an XML serialization.

A web service is an abstract resource that provides a set of functions and is implemented by an agent, which sends and receives messages.

A provider entity provides the functionality of a web service with a provider agent and a requester entity uses the web service functionality with a requester agent.

Web services implement various technologies, some of which are XML, SOAP, and WSDL. XML is a standard format for data exchange.

Web service requests and responses are sent as XML messages.

The elements and attributes that may be specified in an XML document are specified in an XML Schema.

SOAP provides a standard framework for packaging and exchanging XML messages. WSDL is an XML document in the http://schemas.xmlsoap.org/wsdl/ namespace for describing a web service as a set of endpoints operating on messages. A WSDL document specifies the operations (methods) provided by a web service and the format of the XML messages.

Installing the PHP Web Services Extensions

The SOAP and XML-RPC extensions are packaged with the PHP 5 installation. The SOAP extension and the XML-RPC extension are not enabled by default in a PHP installation. To enable the SOAP and XML-RPC extensions add the following extension directives in the php.ini configuration file.

extension=php_xmlrpc.dll
extension=php_soap.dll

REST Context Switching

o Refers to the act of providing different output based on criteria from the request

o The process inspects the http request headers and/or the request uri, and varies the response appropriately

o Commonly used for:

 providing different output for requests originated via xmlhttprequest

 providing different output based on accept http headers (ex: rest endpoints)

 providing alternate layouts/content based on browser detection