downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

HttpRequest::setBody> <HttpRequest::resetCookies
[edit] Last updated: Fri, 17 May 2013

view this page in

HttpRequest::send

(PECL pecl_http >= 0.10.0)

HttpRequest::sendEnviar petición

Descripción

public HttpMessage HttpRequest::send ( void )

Envía la petición HTTP.

Nota:

A pesar de que puede lanzarse una excepción, la transferencia podría haber tenido éxito al menos parcialmente, por lo que quizás desee comprobar los valores devueltos por varias llamadas al método HttpRequest::getResponse*().

Valores devueltos

Devuelve en forma de objeto HttpMessage la respuesta recibida.

Errores/Excepciones

Lanza HttpRuntimeException, HttpRequestException, HttpMalformedHeaderException, HttpEncodingException.

Ejemplos

Ejemplo #1 ejemplo GET

<?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;
}
?>

Ejemplo #2 Ejemplo POST

<?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::setBody> <HttpRequest::resetCookies
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes HttpRequest::send - [3 notes]
up
2
Lymber
3 years ago
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>";
?>
up
0
elio dot cuevas at ge dot com
2 years ago
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));
?>
up
0
mjs at beebo dot org
4 years ago
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);
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites