HttpRequest An multipart POST request : HttpRequest : Development PHP Source Code


PHP Source Code » Development » HttpRequest »

 

HttpRequest An multipart POST request


The following example shows an multipart POST request, with two form fields and an image that's supposed to be uploaded to the server. It's a bad habit as well as common practice to issue a redirect after an received POST request, so we'll allow a redirect by enabling the redirect option.
<?php    
$r 
= new HttpRequest('http://dev.iworks.at/.print_request.php'HTTP_METH_POST);    
    
// if redirects is set to true, a single redirect is allowed;    
// one can set any reasonable count of allowed redirects    
$r->setOptions(    
array( 
'cookies' => array('MyCookie' => 'has a value'),    
'redirect' => true,    
)    
);    
    
// common form data    
$r->setPostFields(    
        array( 
'name' => 'Mike',    
                 
'mail' => 'mike@php.net',    
            )    
);    
// add the file to post (form name, file name, file type)    
touch('profile.jpg');    
$r->addPostFile('image''profile.jpg''image/jpeg');    
    
try {    
print 
$r->send()->getBody();    
} catch (
HttpException $e) {    
print 
$e;    
}    
?>    


HTML code for linking to this page:

Follow Navioo On Twitter

PHP Source Code

 Navioo Development
» HttpRequest