Note that send() does not process redirects, and there doesn't appear to any way to get this to happen automatically. If you need to follow redirects, use something like the following code:
<?php
$request = new HttpRequest($url, HTTP_METH_GET);
do {
$response = $request->send();
if ($response->getResponseCode() != 301 && $response->getResponseCode() != 302) break;
$request->setUrl($response->getHeader("Location"));
} while (1);
?>
HttpRequest::send
(PECL pecl_http >= 0.10.0)
HttpRequest::send — Send request
Description
Send the HTTP request.
Note: While an exception may be thrown, the transfer could have succeeded at least partially, so you might want to check the return values of various HttpRequest::getResponse*() methods.
Return Values
Returns the received response as HttpMessage object.
Errors/Exceptions
Throws HttpRuntimeException, HttpRequestException, HttpMalformedHeaderException, HttpEncodingException.
Examples
Example #1 GET example
<?php
$r = new HttpRequest('http://example.com/feed.rss', HttpRequest::METH_GET);
$r->setOptions(array('lastmodified' => filemtime('local.rss')));
$r->addQueryData(array('category' => 3));
try {
$r->send();
if ($r->getResponseCode() == 200) {
file_put_contents('local.rss', $r->getResponseBody());
}
} catch (HttpException $ex) {
echo $ex;
}
?>
Example #2 POST example
<?php
$r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
$r->setOptions(array('cookies' => array('lang' => 'de')));
$r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
$r->addPostFile('image', 'profile.jpg', 'image/jpeg');
try {
echo $r->send()->getBody();
} catch (HttpException $ex) {
echo $ex;
}
?>
HttpRequest::send
mjs at beebo dot org
01-Apr-2009 06:27
01-Apr-2009 06:27
