Page 1 of 1

How to do a http request using php

Posted: Sat May 01, 2010 1:57 am
by Neo
There are two ways of doing this with php.
  1. PEAR (HTTP_Request)
    See the code samples below.

    Code: Select all

    <?php
    require_once "HTTP/Request.php";
    
    $req =& new HTTP_Request("https://robot.lk/");
    if (!PEAR::isError($req->sendRequest())) {
        echo $req->getResponseBody();
    }
    ?> 
    

    Code: Select all

    <?php
    require_once "HTTP/Request.php";
    
    $req =& new HTTP_Request("https://robot.lk");
    $req->setMethod(HTTP_REQUEST_METHOD_POST);
    $req->addPostData("Foo", "bar");
    if (!PEAR::isError($req->sendRequest())) {
         $response1 = $req->getResponseBody();
    } else {
         $response1 = "";
    }
    
    echo $response1;
    ?> 
    
  2. cURL

    Code: Select all

    $url = "https://robot.lk/";
    
    $request = curl_init();
    curl_setopt($request,CURLOPT_URL,$url);
    curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($request);
    curl_close($request);