Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP HTTP-Request

I have MAMP Pro installed running php 5.2.13. When I try to initialize a HTTP-Request

$r = new HttpRequest('http://example.com/', HttpRequest::METH_GET);

it tells me:

"Class 'HttpRequest' not found in ...".

What do I need to do to 'install(?)' it?

like image 904
JNK Avatar asked Dec 30 '10 19:12

JNK


3 Answers

You must enable http extension:

http://www.php.net/manual/en/http.setup.php

Or you can try new HTTP_Request2:

sudo pear install --alldeps HTTP_Request2-alpha

And then:

$req = new HTTP_Request2('your.url');
$req->setMethod('POST');
$req->setHeader("content-type", $mimeType);
$req->setBody('');
$response = $req->send();
like image 112
Vojta Avatar answered Oct 12 '22 12:10

Vojta


Contemporary Answer for MAMP 2.0 and HTTP_Request2:

Go into your MAMP/bin/php/php5.3.6/bin/ and run

./pear install --alldeps HTTP_Request2

Restart your server and test with the following code, from the PEAR repository:

<?php
require_once 'HTTP/Request2.php';

$request = new HTTP_Request2('http://pear.php.net/', HTTP_Request2::METHOD_GET);
try {
    $response = $request->send();
    if (200 == $response->getStatus()) {
        echo $response->getBody();
    } else {
        echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
             $response->getReasonPhrase();
    }
} catch (HTTP_Request2_Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
?>

Don't forget the require_once statement!

like image 27
Philip Avatar answered Oct 12 '22 13:10

Philip


You need to enable the extension ...

add the following to your php.ini

extension = php_http.dll

Apparently that was asked a lot:

http://php.bigresource.com/Track/php-33sNme7A/

like image 2
Andrey Voev Avatar answered Oct 12 '22 11:10

Andrey Voev