Search code examples
phphttp-headersgetpearput

PHP HTTP header REST


I am developing an UI for a REST repository using PHP and the PEAR HTTP REQUEST package (http://pear.php.net/package/HTTP_Request/). I created a HTTP GET request and it delivers the requested rdf/xml file as expected. But I want to extend this request and I can't get this working. The repository allows sending zip files which are attached to an id. So I have to call the same URL which delivers the rdf/xml data, but I have to change the HTTP GET header from xml to accept: application/zip, before executing my request. This should deliver the zip instead of the rdf/xml file.

$req =& new HTTP_Request();
$req->setMethod(HTTP_REQUEST_METHOD_GET);
$req->setURL($url);
$req->clearPostData();
if (!PEAR::isError($req->sendRequest())) {
     $response2 = $req->getResponseBody();
} else {
     $response2 = "";
}

echo $response2;

Does anyone know how to modify the GET call to get this done? I really need help!

Furthermore I want to create a HTTP PUT request which uses multipart/form-data. Does anyone know how to make this?

Please help me! Thanks!


Solution

  • For your first question, you can set the Accept field of your GET request header by:

    $req->addHeader('Accept', 'application/zip');
    # assuming that this will trigger the server to respond with the zip and not xml
    

    Question number 2:

    # Set method to PUT
    $req->setMethod(HTTP_REQUEST_METHOD_PUT);
    
    # Attach file to request
    $req->addFile('file_upload_field', '/path/to/file.ext', 'application/zip');
    

    Read up more on file uploads using HTTP_Request.