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;
}
?>
[an error occurred while processing the directive]
- Функция HttpRequest::addCookies() - Add cookies
- Функция HttpRequest::addHeaders() - Add headers
- Функция HttpRequest::addPostFields() - Add post fields
- Функция HttpRequest::addPostFile() - Add post file
- Функция HttpRequest::addPutData() - Add put data
- Функция HttpRequest::addQueryData() - Add query data
- Функция HttpRequest::addRawPostData() - Add raw post data
- Функция HttpRequest::addSslOptions() - Add ssl options
- Функция HttpRequest::clearHistory() - Clear history
- Функция HttpRequest::__construct() - HttpRequest constructor
- Функция HttpRequest::enableCookies() - Enable cookies
- Функция HttpRequest::getContentType() - Get content type
- Функция HttpRequest::getCookies() - Get cookies
- Функция HttpRequest::getHeaders() - Get headers
- Функция HttpRequest::getHistory() - Get history
- Функция HttpRequest::getMethod() - Get method
- Функция HttpRequest::getOptions() - Get options
- Функция HttpRequest::getPostFields() - Get post fields
- Функция HttpRequest::getPostFiles() - Get post files
- Функция HttpRequest::getPutData() - Get put data
- Функция HttpRequest::getPutFile() - Get put file
- Функция HttpRequest::getQueryData() - Get query data
- Функция HttpRequest::getRawPostData() - Get raw post data
- Функция HttpRequest::getRawRequestMessage() - Get raw request message
- Функция HttpRequest::getRawResponseMessage() - Get raw response message
- Функция HttpRequest::getRequestMessage() - Get request message
- Функция HttpRequest::getResponseBody() - Get response body
- Функция HttpRequest::getResponseCode() - Get response code
- Функция HttpRequest::getResponseCookies() - Get response cookie(s)
- Функция HttpRequest::getResponseData() - Get response data
- Функция HttpRequest::getResponseHeader() - Get response header(s)
- Функция HttpRequest::getResponseInfo() - Get response info
- Функция HttpRequest::getResponseMessage() - Get response message
- Функция HttpRequest::getResponseStatus() - Get response status
- Функция HttpRequest::getSslOptions() - Get ssl options
- Функция HttpRequest::getUrl() - Get url
- Функция HttpRequest::resetCookies() - Reset cookies
- Функция HttpRequest::send() - Send request
- Функция HttpRequest::setBody() - Set request body to send, overwriting previously set request body.
- Функция HttpRequest::setContentType() - Set content type
- Функция HttpRequest::setCookies() - Set cookies
- Функция HttpRequest::setHeaders() - Set headers
- Функция HttpRequest::setMethod() - Set method
- Функция HttpRequest::setOptions() - Set options
- Функция HttpRequest::setPostFields() - Set post fields
- Функция HttpRequest::setPostFiles() - Set post files
- Функция HttpRequest::setPutData() - Set put data
- Функция HttpRequest::setPutFile() - Set put file
- Функция HttpRequest::setQueryData() - Set query data
- Функция HttpRequest::setRawPostData() - Set raw post data
- Функция HttpRequest::setSslOptions() - Set ssl options
- Функция HttpRequest::setUrl() - Set URL
Коментарии
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);
?>
Example how to use HttpRequest to post data and receive the response:
<?php
//set up variables
$theData = '<?xml version="1.0"?>
<note>
<to>php.net</to>
<from>lymber</from>
<heading>php http request</heading>
<body>i love php!</body>
</note>';
$url = 'http://www.example.com';
$credentials = 'user@example.com:password';
$header_array = array('Expect' => '',
'From' => 'User A');
$ssl_array = array('version' => SSL_VERSION_SSLv3);
$options = array(headers => $header_array,
httpauth => $credentials,
httpauthtype => HTTP_AUTH_BASIC,
protocol => HTTP_VERSION_1_1,
ssl => $ssl_array);
//create the httprequest object
$httpRequest_OBJ = new httpRequest($url, HTTP_METH_POST, $options);
//add the content type
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
//add the raw post data
$httpRequest_OBJ->setRawPostData ($theData);
//send the http request
$result = $httpRequest_OBJ->send();
//print out the result
echo "<pre>"; print_r($result); echo "</pre>";
?>
The HttpRequest class does support redirects, but it's disabled by default. To enable redirect do something like this:
<?php
// Set number of redirects to a reasonable number.
$r->setOptions(array('redirect' => 10));
?>
There is a typo in Lymber's comment.
Instead of
$httpRequest_OBJ->setContentType = 'Content-Type: text/xml';
use
$httpRequest_OBJ->setContentType('text/xml');