POST Requests

<?php
$data = array(
'param1' => 'value1', 'param2' => 'value2'
);

$files = array( array(
'name' => 'filel',
'type' => 'image/jpeg',
'file' => '/path/to/filel.jpg'
),
array(
'name' => 'file2',
'type' => 'image/gif',
'file' => '/path/to/file2.gif'
)
);

$options = array(
'referer' => 'http://localhost.example/'
);
// Procedural

$response = http_post_fields(
'http://localhost.example/process',
$data,
$files,
$options,
$info
);
// Object-oriented $request = new HttpRequest;

$request->setU rl('http://localhost.example/process');
$request->setMethod(HTTP_METH_POST);
$request->setPostFields($data);
$request->setPostFiles($files);
$request->setOptions($options);
$response = $request->send();
?>
  • http_post_fields, setPostFields, and setPostFiles are used to set the request method to POST and to specify that the extension should handle constructing and encoding the POST body with the provided data. If you are handling this aspect of the request yourself, use http_post_data or setRawPostData instead.
  • setUrl is used to set the URL of the HttpRequest instance rather than the constructor, just as an example.
  • After HttpRequest is instantiated, its request method is explicitly set using the constant HTTP_METH_POST.
  • Request options are passed in this time, specifically the value for the Referer header. They are specified as the fourth parameter of http_post_fields and via setOptions.
  • As with the GET request, the return values of http_post_fields and send are of the same types (string and HttpMessage) as their respective counterparts in the earlier GET example.
  • Because http_post_fields does not have an object to use in retaining state, it instead allows the option to pass in a fifth parameter $info in which it will store information about the request and response. See http: //php. net/http_get for an example of the information stored in $info.

Alternative Methods and More Options

For request method constants beyond GET and POST used by the procedural API, see http: //php. net/manual/en/http. constants. php. For more on available request options for both APIs, see http://php. net/manual/en/http. request .options.php. 


© pecl_http PECL Extension — Web Scraping

>>> Back to TABLE OF CONTENTS <<<
Category: Article | Added by: Marsipan (30.08.2014)
Views: 448 | Rating: 0.0/0
Total comments: 0
avatar