PHP code example of initphp / curl

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

    

initphp / curl example snippets



use \InitPHP\Curl\Curl;

$curl = new Curl();
$curl->setUrl("https://example.com");
$curl->handler();
    
$res = $this->getResponse();

if(!empty($curl->getError())){
    die($curl->getError());
}
echo $res['body'];


use \InitPHP\Curl\Curl;

$curl = Curl::client('PUT', 'http://api.service.example.com/update/1')

$curl->setVersion("1.1") // HTTP Version
    ->setHeader("Content-Type", "application/json")
    ->setBody(json_encode([
        'username'  => 'admin',
        'password'  => '12345',
    ]))
    ->handler();

if(!empty($curl->getError())){
    die($curl->getError());
}

switch ($curl->getResponse('code')) {
    case 200 :
    case 201 :
        // Success
        break;
    case 404 :
        // Not Found
        break;
    case 400:
        // Badrequest
        break;
    // ...
}

public static function client(string $method, string $url): \InitPHP\Curl\Curl;

public function getResponse(null|string $key = null): null|mixed;

public function setUrl(string $url): self

public function setHeader(string $name, string $value): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setHeader('Content-type', 'application/json; charset=utf8');

public function setHeaders(array $headers): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setHeaders([
    'Content-type' => 'application/json; charset=utf8'
]);

public function setMethod(string $method = 'GET'): self

public function setBody(string $body): self

public function setVersion(string $httpVersion = '1.1'): self

public function setUserAgent(null|string $userAgent = null): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");

public function setReferer(null|string $referer = null): self

public function setAllowRedirect(int $maxRedirect = 3): self

public function setTimeout(int $timeout = 0, bool $isMicrosecond = false): self

public function setField(string $key, string $value): self

public function setFields(array $fields) : self

public function setSSL(int $host = 2, int $verify = 1, null|int $version = null): self

public function setProxy($proxy): self

public function setUpload(string $name, \CURLFile $file): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setUpload("upload_file", new \CURLFile(__DIR__ . 'image.jpg')); // $_FILES["upload_file"]

/** @var \InitPHP\Curl\Curl $curl */
$curl->setUpload("upload_file[0]", new \CURLFile(__DIR__ . 'image-1.jpg'));
$curl->setUpload("upload_file[1]", new \CURLFile(__DIR__ . 'image-2.jpg'));

public function getInfo(null|string $key = null): null|mixed

public function getError(): null|string

public function setOpt(int $key, mixed $value): self

public function setOptions(array $options): self

public function handler(): bool

public function save(string $filePath): false|int

/** @var \InitPHP\Curl\Curl $curl */
$curl = new \InitPHP\Curl\Curl();
$curl->setUrl("http://example.com")
        ->handler();
        
if($curl->save(__DIR__ . '/example.html') === FALSE){
    echo "The file could not be written.";
}

public function setCookie(string $name, string|int|float $value): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setCookie('username', 'admin') // $_COOKIE['username']
    ->setCookie('mail', '[email protected]'); // $_COOKIE['mail']

public function setCookies(array $cookies): self

/** @var \InitPHP\Curl\Curl $curl */
$curl->setCookies([
    'username'  => 'admin', // $_COOKIE['username']
    'mail'      => '[email protected]' // $_COOKIE['mail']
]);

public function setCookieJarFile(string $filePath): self

$login = new \InitPHP\Curl\Curl();
$login->setUrl("http://example.com/user/login")
    ->setField('username', 'admin')
    ->setField('password', '123456')
    ->setMethod('POST')
    ->setCookieJarFile(__DIR__ . '/session.txt')
    ->handler();
    
$dashboard = new \InitPHP\Curl\Curl();
$dashboard->setUrl("http://example.com/user/dashboard")
    ->setCookieJarFile(__DIR__ . '/session.txt')
    ->handler();