PHP code example of rakit / curl

1. Go to this page and download the library: Download rakit/curl library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rakit / curl example snippets


use Rakit\Curl\Curl;

$request = new Curl('http://wikipedia.com');
$response = $request->get();

// then, you can do something with response object
if(! $response->error()) {

  // getting response file content
  $html = $response->getBody();
  
  // simply get response content type
  $content_type = $response->getContentType(); 
  
  // get content type via curl info
  $content_type = $response->getInfo('content_type'); 
  
  // getting response cookies
  $cookie = $response->getCookie();
  
  // simply get response header item
  $http_version = $response->getHeader('http_version');
  
  // get all header items
  $all_headers = $response->getHeaders();

} else {

  $error_code = $response->getErrno();
  $error_message = $response->getErrorMessage();

}

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/products');
$request->param('page', 2);
$request->param('category', 'software'); 
$response = $request->get();

// do something with Response object


use Rakit\Curl\Curl;

$params = array(
  'page' => 2,
  'category' => 'software'
);

$request = new Curl('http://targetsite.com/products');
$response = $request->get($params);

// do something with Response object


use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', '[email protected]');
$request->param('password', '12345');
$response = $request->post();

// do something with Response object


use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/register');
$request->param('name', 'John Doe');
$request->param('email', '[email protected]');
$request->param('password', '12345');

$request->addFile('avatar', 'path/to/avatar.png');

$response = $request->post();

// do something with Response object

use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/products');
$request->cookie('key', 'value');
$response = $request->get();

// do something with Response object


use Rakit\Curl\Curl;

$request = new Curl('http://targetsite.com/admin/product/delete/1');
$request->autoRedirect(5); // 5 is maximum redirection

$response = $request->get();

// what if 6th request is also redirection? it's good to check
if($response->isRedirect()) {
    throw new \Exception("Too many redirection");
}

// do something with response

use Rakit\Curl\Curl;

$session_file = "path/to/store/sitetarget.session";

$request = new Curl("http://sitetarget.com/login");
$request->autoRedirect(5);
$request->storeSession($session_file);

$response = $request->post(array(
    'username' => 'my_username',
    'password' => 'my_password'
));

// do something with response object

use Rakit\Curl\Curl;

// simple GET
$response = Curl::doGet('http://targetsite.com', array('page' => 2));

// simple POST 
$response = Curl::doPost('http://targetsite.com/product/delete', array('id' => 5));

$response = Curl::doGet("http://sitetarget.com/blablabla");

if($response->isNotFound()) {
   // 404 page not found
}

// redirecting example

$response = Curl::doGet("http://sitetarget.com/redirect-me");

while($response->isRedirect()) {
    $redirect_url = $response->getHeader("location");
    $response = Curl::doGet($redirect_url);
}


$response = Curl::doGet("http://sitetarget.com");

$http_version = $response->getHeader("http_version");
$content_type = $response->getHeader("content-type");
// and many more