PHP code example of 0x1881 / curl

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

    

0x1881 / curl example snippets




 = new \C4N\Curl;
$curl->get('https://httpbin.org/get')
     ->setDebug(true)
     ->setHeader('User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)')
     ->setUserAgent('Googlebot/2.1 (+http://www.google.com/bot.html)')
     ->setCookieFile('./cookie.txt')
     ->setCookieJar('./cookie.txt')
     ->setReferer('https://httpbin.org/')
     ->setAutoReferer(true)
     ->setTimeout(5)
     ->setProxy('127.0.0.1:8888')
     ->setProxyAuth('user:pass')
     ->exec();

echo $curl->getResponse();
echo $curl->getHttpCode();
echo $curl->getHeader('Location');
echo $curl->getCookie('laravel_session');

$curl->setDefault();

$curl->setMethod('GET');

$curl->setUrl('https://httpbin.org/get');

$curl->setHeader('Test-Header: value');

$curl->setHeader('Test-Header', 'value');

$headers = [
    'Test-Header: value',
    'Test-Header2: value'
];
$curl->setHeader($headers);

$curl->setBody('name=Mehmet&lastname=Can');

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->setBody($body);

// name=Mehmet&lastname=Can

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->setBody($body, $curl::QUERY);

// name=Mehmet&lastname=Can

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->setBody($body, $curl::JSON);

// {"name":"Mehmet","lastname":"Can"}

$curl->setOpt(CURLOPT_URL, 'https://httpbin.org/get');

$curl->setDebug(true);

$curl->setUserAgent('Googlebot/2.1 (+http://www.google.com/bot.html)');

$cookies = "XSRF-TOKEN=OWY4NmQwODE4ODRMjJjZDE1ZDZMGYwMGEwOA==; ci_session=esa2tb3mviicp2cb5abz32g";
$curl->setCookie($cookies);

$cookie_name = 'XSRF-TOKEN';
$cookie_value = 'OWY4NmQwODE4ODRMjJjZDE1ZDZMGYwMGEwOA==';
$curl->setCookie($cookie_name, $cookie_value);

$cookies = [
    'XSRF-TOKEN' => 'OWY4NmQwODE4ODRMjJjZDE1ZDZMGYwMGEwOA==',
    'ci_session' => 'esa2tb3mviicp2cb5abz32g'
];
$curl->setCookie($cookies);

$curl->setCookieFile(__DIR__.DIRECTORY_SEPARATOR.'cookies.txt');

$curl->setCookieJar(__DIR__.DIRECTORY_SEPARATOR.'cookies.txt');

$curl->setFollow(true);

$curl->setReturn(true);

$curl->setReferer('https://httpbin.org/');

$curl->setAutoReferer(true);

$curl->setTimeout(5);

$curl->setConnectTimeout(5);

$curl->setMaxConnect(5);

$curl->setMaxRedirect(5);

$curl->setProxy('127.0.0.1', '8080');

$curl->setProxy('127.0.0.1:8080');

$curl->setProxy('http://127.0.0.1:8080');
$curl->setProxy('https://127.0.0.1:8080');
$curl->setProxy('socks4://127.0.0.1:8080');
$curl->setProxy('socks5://127.0.0.1:8080');

$curl->setProxy('username:[email protected]:8080');
$curl->setProxy('http://username:[email protected]:8080');
$curl->setProxy('https://username:[email protected]:8080');
$curl->setProxy('socks4://username:[email protected]:8080');
$curl->setProxy('socks5://username:[email protected]:8080');

$curl->setProxyType(CURLPROXY_HTTPS);

$curl->setProxyAuth('user:pass');

$curl->setProxyAuth('user', 'pass');

$curl->setProxyAuth('user');

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->send('GET', 'https://httpbin.org/get', $headers);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->send('POST', 'https://httpbin.org/get', $headers, $body, $curl::JSON);

$curl->get('https://httpbin.org/get');

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->get('https://httpbin.org/get', $headers);

$headers = 'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html';

$curl->get('https://httpbin.org/get', $headers);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->post('https://httpbin.org/post', $headers, $body);
$curl->post('https://httpbin.org/post', $headers, $body, $curl::JSON);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->put('https://httpbin.org/put', $headers, $body);
$curl->put('https://httpbin.org/put', $headers, $body, $curl::JSON);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->put('https://httpbin.org/put', $headers, $body);
$curl->put('https://httpbin.org/put', $headers, $body, $curl::JSON);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->delete('https://httpbin.org/delete', $headers, $body);
$curl->delete('https://httpbin.org/delete', $headers, $body, $curl::JSON);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$body = [
    'name' => 'Mehmet',
    'lastname' => 'Can'
];

$curl->patch('https://httpbin.org/patch', $headers, $body);
$curl->patch('https://httpbin.org/patch', $headers, $body, $curl::JSON);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->head('https://httpbin.org/head', $headers);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->connect('https://httpbin.org/connect', $headers);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->options('https://httpbin.org/options', $headers);

$headers = [
    'User-agent: Googlebot/2.1 (+http://www.google.com/bot.html)'
];

$curl->trace('https://httpbin.org/options', $headers);

$curl->exec();

$curl->getInfo();

/*
Array
(
    [url] => https://httpbin.org/get
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 202
    [request_size] => 51
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.725079
    [namelookup_time] => 0.126243
    [connect_time] => 0.271622
    [pretransfer_time] => 0.577915
    [size_upload] => 0
    [size_download] => 221
    [speed_download] => 304
    [speed_upload] => 0
    [download_content_length] => 221
    [upload_content_length] => -1
    [starttransfer_time] => 0.724962
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 127.0.0.1
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 127.0.0.1
    [local_port] => 22219
    [http_version] => 3
    [protocol] => 2
    [ssl_verifyresult] => 0
    [scheme] => HTTPS
    [appconnect_time_us] => 577763
    [connect_time_us] => 271622
    [namelookup_time_us] => 126243
    [pretransfer_time_us] => 577915
    [redirect_time_us] => 0
    [starttransfer_time_us] => 724962
    [total_time_us] => 725079
)
*/

$curl->getInfo('http_code');


$curl->getCurlError();

$curl->getResponse();

$curl->getResponse(true);

$curl->getRespJson();

$curl->getRespJson(true);

$curl->getRespJson(false, JSON_PRETTY_PRINT);

$curl->getEffective();

// https://httpbin.org/get

$curl->getHttpCode();

// 200

$curl->getHeader('device_id');

// e324h4e708f097febb40384a51as48

$curl->getHeader('device_id', 0);

// e324h4e708f097febb40384a51as48

$curl->getHeaders();

/*
Array
(
    [response_code] => 200
    [Date] => Sat, 21 May 2022 10:00:34 GMT
    [Content-Type] => application/json
    [Content-Length] => 291
    [Connection] => keep-alive
    [Server] => gunicorn/19.9.0
    [Access-Control-Allow-Origin] => *
    [Access-Control-Allow-Credentials] => true
)
*/

$curl->getHeaders(0);

$curl->getCookie('XSRF-TOKEN');

// OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZMGYwMGEwOA==

$curl->getCookie('XSRF-TOKEN', 0);

// OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZMGYwMGEwOA==

$curl->getCookiesRaw();

// laravel_session=eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=

$curl->getCookiesRaw(0);

// laravel_session=eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=

$curl->getCookiesArray();

/*
Array
(
    [laravel_session] => eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=
)
*/

$curl->getCookiesArray(0);

/*
Array
(
    [laravel_session] => eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=
)
*/


// <p>asd</p>

$curl->getBetween('<p>', '</p>');

//asd

$curl->getBetween('<p>', '</p>', true);

/*
<p>test</p>
<p>test 2</p>
*/

$curl->getBetweens('<p>', '</p>');
/*
Array
(
    [0] => test
    [1] => test 2
)
*/

// <p>test</p><p>test 2</p>
$curl->getBetweens('<p>', '</p>', true);
/*
Array
(
    [0] => test
    [1] => test 2
)
*/

$find = $curl->find("<title>Homepage</title>");

if ($find->result) {
    echo 'Bulunan: '.$find->finded;
}

/*
stdClass Object
(
    [result] => 1
    [finded] => <title>Anasayfa</title>
)
*/

$found = [
    "<title>Homepage</title>",
    "Homepage",
    "Topic is added.",

];
$find = $curl->find($found);

if ($find->result) {
    echo 'Founds: '; print_r($find->finded);
    
}

/*
stdClass Object
(
    [result] => 1
    [finded] => Array
        (
            [0] => <title>Homepage</title>
            [1] => Homepage
        )

)
*/

$found = [
    "<title>Homepage</title>",
    "Homepage",
    "Topic is added.",

];

$string = "<title>Homepage</title>Topic is added.";

$find = $curl->find($found, $string);

if ($find->result) {
    echo 'Found: '; print_r($find->finded);
    
}

/*
stdClass Object
(
    [result] => 1
    [finded] => Array
        (
            [0] => <title>Homepage</title>
            [1] => Homepage
        )

)
*/

$curl->getOpt();

/*
Array
(
    [42] => 1
    [19913] => 1
    [10036] => GET
    [10002] => https://www.google.com/
    [80] => 1
    [84] => 2
)
*/

$curl->getOpt(CURLOPT_URL);

// https://www.google.com/

$curl::RAW;

$curl::JSON;

$curl::QUERY;
POST