PHP code example of phpgt / curlinterface

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

    

phpgt / curlinterface example snippets


$curl = new Curl("https://catfact.ninja/fact");
$curl->exec();
$json = $curl->outputJson();
echo "Here's a cat fact: {$json->getString("fact")}";
echo PHP_EOL;
echo "The fact's length is {$json->getInt("length")} characters.";
echo PHP_EOL;

// Using native functionality to achieve the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://catfact.ninja/fact");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
if(false === $result) {
	die("CURL error: " . curl_error($ch));
}
$json = json_decode($result);
if(is_null($json)) {
	die("JSON decoding error: " . json_last_error_msg());
}

// Note: No type checks are made on the `fact` and `length` properties here.
echo "Here's a cat fact: {$json->fact}";
echo PHP_EOL;
echo "The fact's length is {$json->length} characters.";
echo PHP_EOL;