Recipe 13.5. Fetching a URL with an Arbitrary Method


13.5.1. Problem

You want to retrieve a URL using a method more exotic than get or post, such as put or delete.

13.5.2. Solution

Just as when using post, set the method and content stream context options when using the http stream, as in Example 13-24.

Using put with the http stream

<?php $url = 'http://www.example.com/put.php'; // The request body, in arbitrary format $body = '<menu>  <dish type="appetizer">Chicken Soup</dish>  <dish type="main course">Fried Monkey Brains</dish> </menu>'; $options = array('method' => 'PUT', 'content' => $body); // Create the stream context $context = stream_context_create(array('http' => $options)); // Pass the context to file_get_contents() print file_get_contents($url, false, $context); ?>

With cURL, set the CURLOPT_CUSTOMREQUEST option to the method name. To include a request body, set CURLOPT_POSTFIELDS to the the body, as in Example 13-25.

Using put with cURL

<?php // The request body, in arbitrary format $body = '<menu>  <dish type="appetizer">Chicken Soup</dish>  <dish type="main course">Fried Monkey Brains</dish> </menu>'; $c = curl_init($url); curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'PUT'); curl_setopt($c, CURLOPT_POSTFIELDS, $body); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($c); curl_close($c); ?>

Example 13-26 shows how to put with HTTP_Request: pass HTTP_REQUEST_METHOD_PUT to the constructor and call setBody( ) with the contents of the request body.

Using put with HTTP_Request

<?php require 'HTTP/Request.php'; $url = 'http://www.example.com/put.php'; $body = '<menu>  <dish type="appetizer">Chicken Soup</dish>  <dish type="main course">Fried Monkey Brains</dish> </menu>'; $r = new HTTP_Request($url); $r->setMethod(HTTP_REQUEST_METHOD_PUT); $r->setBody($body); $page = $r->getResponseBody(); ?>

13.5.3. Discussion

As REST-style web services APIs grow more common, so do HTTP requests using lesser lights of the request-method pantheon, such as put and delete.

The put method is often used for uploading the contents of a particular file. cURL has three special options to help with this: CURLOPT_PUT, CURLOPT_INFILE, and CURLOPT_INFILESIZE. To upload a file with put and cURL, set CURLOPT_PUT to true, CURLOPT_INFILE a filehandle opened to the file that should be uploaded, and CURLOPT_INFILESIZE to the size of that file. Example 13-27 uploads a file with put.

Uploading a file with cURL and put

<?php $url = 'http://www.example.com/upload.php'; $filename = '/usr/local/data/pictures/piggy.jpg'; $fp = fopen($filename,'r'); $c = curl_init($url); curl_setopt($c, CURLOPT_PUT, true); curl_setopt($c, CURLOPT_INFILE, $fp); curl_setopt($c, CURLOPT_INFILESIZE, filesize($filename)); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $page = curl_exec($c); print $page; curl_close($c); ?> 

13.5.4. See Also

Documentation on curl_setopt( ) at http://www.php.net/curl-setopt and on stream options at http://www.php.net/wrappers.http; the PEAR HTTP_Request class at http://pear.php.net/package/HTTP_Request; Section 5.1.1 of RFC 2616, which discusses request methods, is available at http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 .




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net