PHP code example of vulcanphp / easycurl

1. Go to this page and download the library: Download vulcanphp/easycurl 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/ */

    

vulcanphp / easycurl example snippets



// index.php

use VulcanPhp\EasyCurl\EasyCurl;

uest
var_dump(
    EasyCurl::get('https://jsonplaceholder.typicode.com/posts')
        ->getJson()
);

// can make every http request statically
var_dump(
    EasyCurl::put('https://jsonplaceholder.typicode.com/posts/1')
        ->getBody()
);

// create a Easy Curl instance
$http = EasyCurl::create();

// set curl options
$http->setOption(CURLOPT_TIMEOUT, 20);

// set header
$http->setHeader('Authorization', 'Bearer {token}');

// set postfields 
$http->setPostFields(['name' => 'John Doe', 'email' => '[email protected]']);

// send this request
$resp = $http->post('http://your-location.domain/path');

// print the response
var_dump($resp);

// ...


// index.php
use VulcanPhp\EasyCurl\EasyCurl;

code.com/posts');

// get the curl output in array
var_dump($resp->getResponse());

// get the curl status
var_dump($resp->getStatus());

// get response body
var_dump($resp->getBody());

// if the output is json format we can convert it with array
var_dump($resp->getJson()); 

// get output content length
var_dump($resp->getLength());

// get curl last effective url
var_dump($resp->getLastUrl());

// Tip: all methods can be called without get word, EX: $resp->body() is equal to $resp->getBody()


// index.php

use VulcanPhp\EasyCurl\EasyCurl;

ic methods
$http = EasyCurl::options([
    CURLOPT_TIMEOUT => 20,
    // all curl_setopt() options 
]);

// send method is equal to get method, or you can use any http methods
$resp = $http->send('http://domain.com/path');

// download a file
$status = EasyCurl::downloadFile(__DIR__ . '/test.txt')
    ->get('http://domain.com/path')
    ->status();

var_dump($status);

// setup a proxy in easy curl
$http = EasyCurl::proxy([
    'ip'    => '127.0.0.1', // proxy ip
    'port'  => '8080', // proxy port
    'auth'  => 'user:pass', // basic proxy auth
]);

var_dump($http->get('http://domain.com/path'));

// ...