PHP code example of alioygur / curl

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

    

alioygur / curl example snippets



use Alioygur\Curl\Curl;

$curl = new Curl();

$response = $curl
    ->setOption('CURLOPT_FOLLOW_REDIRECTS', false)
    ->setHeader('User-Agent', 'My Name is Heisenberg!')
    ->get('http://example.com');

$response = $curl->head($url, $vars = []);
$response = $curl->get($url, $vars = []); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = []);
$response = $curl->put($url, $vars = []);
$response = $curl->delete($url, $vars = []);

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = []);

function post($url, $vars = []) {
    return $this->request('POST', $url, $vars);
}

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

# Response Headers -------------------------------------------------------------------------

# Get the response body
echo $response->body(); # A string containing everything in the response except for the headers

# Get the response headers
print_r($response->headers()); # An associative array containing the response headers

# Pick one from response headers
echo $response->headers('Content-Type'); # text/html 

# You can also use those methods 
$response->status(); # 200 OK
$response->statusCode(); # 200
$response->ContentType(); # text/html

# Request Headers --------------------------------------------------------------------------

# Get the request headers
$response->requestHeaders(); # An associative array containing the request headers

# Pick one from request headers 
echo $response->requestHeaders('Version'); # HTTP/1.1

# Curl Information -------------------------------------------------------------------------
Get information regarding a specific transfer. See, http://php.net/manual/en/function.curl-getinfo.php

# Get all
$response->getInfo(); # An associative array containing the curl information

# Pick one
$response->getInfo('total_time'); # 0.14257

$curl->setCookieFile('some_other_filename');

$curl->setheader('SOME_KEY', 'some value');

# you can also method chaining
$response = $curl->setHeader('Content-Type', 'application/json')
     ->setHeader('User-Agent', 'Mozilla/5.0 (X11; Linux...')
     ->get('http://example.com');

$curl->setOptions('CURLOPT_FOLLOW_REDIRECTS', false);

$curl->setAuth('username', 'password');