Simple PHP cUrl Get API
<?php
use Kanxpack\CurlGet\CurlGet;
require_once './vendor/autoload.php';
echo CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResult();
print_r(CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResultArray());
$curlArray = CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResultArray();
print_r($curlArray[0]['name']['common']);composer require kanxpack/curlget
{
"require": {
"kanxpack/curlget": "^1.0"
}
}
<?php
require 'vendor/autoload.php';
use Kanxpack\CurlGet\CurlGet;
echo CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResult();
print_r(CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResultArray());Why are you not using composer? Download the CurlGet latest release and put the contents of the ZIP archive into a directory in your project. Then require the file autoload.php to get all classes and dependencies loaded on need.
<?php
require 'path-to-CurlGet-directory/autoload.php';
use Kanxpack\CurlGet\CurlGet;
echo CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResult();
print_r(CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResultArray());The CurlGet class handles the cURL Get request in PHP.
<?php
namespace Kanxpack\CurlGet;
class CurlGet {
// code here
}You can see from the code snippet above that the CurlGet class is declared in the Kanxpack\CurlGet namespace. You need to import the namespace to use CurlGet without having to provide its fully qualified name each time.
use CurlGet\CurlGet;Examples in this documentation will assume you imported classes of the Kanxpack\CurlGet namespace this way.
echo CurlGet::get('https://restcountries.com/v3.1/name/portugal')->getResult();
// This is a a Curl Get request that fetches the API JSON string from https://restcountries.com/v3.1/name/portugal
$postcodeArray = CurlGet::get('https://api.postcodes.io/postcodes/SW1A2AA')->getResultArray();
print_r($postcodeArray);
// This is Curl Get request that fetches the API JSON string and then converts it to an array from https://api.postcodes.io/postcodes/SW1A2AApublic static function get(string $url, array $get = array(), array $options = array()) : self
{
self::setDefaults($url, $get);
self::initialiseHandle();
self::setOptionsArray($options);
self::executeSession();
return self::getInstance();
}This is the main static method that handles the CUrl Get request and it returns an instance of itself.
public static function getResult() : string|false
{
return self::$result;
}This static method should be called to return the default result from the get() method. Usually the result is a string response.
public static function getResultArray() : array
{
return json_decode(self::getResult(), true);
}This static method should be called to return the default result from the get() method as an arrat instead.
This project exists thanks to all the people who contribute.
Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
- This is a donation. No goods or services are expected in return. Any requests for refunds for those purposes will be rejected.