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->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