PHP Classes

PHP Curl Class Wrapper For Bots: Send HTTP requests to given site using Curl

Recommend this page to a friend!
  Info   View files Documentation   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 72 This week: 1All time: 10,212 This week: 560Up
Version License PHP version Categories
curl-0x1881 1.0The PHP License7HTTP, PHP 5
Collaborate with this project 

Author

curl - github.com

Description

This package can send HTTP requests to a given site using Curl.

It provides functions that can take several types of parameters to configure an HTTP request to that the class will send to an HTTP server.

After setting all the parameters, the class can execute sending of the configured HTTP request.

Picture of 0x1881
  Performance   Level  
Name: 0x1881 <contact>
Classes: 2 packages by
Country: Turkey Turkey
Age: 26
All time rank: 423979 in Turkey Turkey
Week rank: 416 Up9 in Turkey Turkey Up
Innovation award
Innovation award
Nominee: 1x

Documentation

PHP Curl

en tr

This package can send HTTP requests to a given site using Curl.

It provides functions that can take several types of parameters to configure an HTTP request to that the class will send to an HTTP api.

After setting all the parameters, the class can execute sending of the configured HTTP request.

Coded for educational purposes. The user is responsible for the abuse.

---

Install

composer require 0x1881/curl

---

Using

<?php

require_once __DIR__.'/vendor/autoload.php';

$curl = 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');

---

Methods

Request Methods

Response Methods

Other Methods

Constants

Detailed Description of Methods

Details of Request Methods

setDefault()

Returns the class to its default request settings. It resets the settings to default on every request made.

$curl->setDefault();

setMethod()

Used to specify the request type.

Kabul edilen türler

  • 
    
$curl->setMethod('GET');

setUrl()

Specifies the destination of the request.

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

setHeader()

Adds header to the request.

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

or

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

or

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

setBody()

Adds body to the request. Applies to certain request methods.

Raw: The specified body is set for the request as direct plain text.

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

Query 1: Sets a body of content type `x-www-form-urlencoded`. Added as an example comment. If the data is of type array, the method will convert it to the format we specified.

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

$curl->setBody($body);

// name=Mehmet&lastname=Can

Query 2: Sets a body of content type `x-www-form-urlencoded`. Added as an example comment.

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

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

// name=Mehmet&lastname=Can

_Json_: Sets a body in `json` format. Added as an example comment.

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

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

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

setOpt()

It is the method that makes the `curl_setopt` method of the curl library practical. CURLOPT constants can be used optionally.

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

setDebug()

It sets the constant CURLOPT_VERBOSE from the curl library to true, so it returns optional debug data on terminal lines. The default `false` is off.

$curl->setDebug(true);

setUserAgent()

Sets the `User-Agent` header of the request.

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

setCookie()

Adds the specified cookie information to the `Cookie` header of the request.

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

or

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

or

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

setCookieFile()

It takes the cookie data from the file with the cookie information in Netscape format and adapts it to the request.

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

setCookieJar()

It adds and saves the cookie information in Netscape format to the specified file.

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

setFollow()

Used to allow if the request is redirecting. The default `false` is off.

$curl->setFollow(true);

setReturn()

Allows the request transfer to be output or not. The default `false` is off.

$curl->setReturn(true);

setReferer()

Sets the `Referer` header to use in the request.

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

setAutoReferer()

If the request has redirected, it automatically sets the `Refererheader to be used in the request. The defaultfalse` is off.

$curl->setAutoReferer(true);

setTimeout()

Sets the timeout of curl functions in seconds.

$curl->setTimeout(5);

setConnectTimeout()

Sets the attempt time, in seconds, of the request.

$curl->setConnectTimeout(5);

setMaxConnect()

Sets the maximum number of simultaneous connections.

$curl->setMaxConnect(5);

setMaxRedirect()

Sets the maximum number of redirects. The default value is `20`.

$curl->setMaxRedirect(5);

setProxy()

It allows the request to be connected via proxy. It can auto parse. Only the 1st argument should be used when auto-parsing. It uses the default `HTTPS` proxy type.

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

veya (oto ayr??t?l?r)

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

or proxy type can be specified. (auto parse)

$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');

or authentication with username and password. (auto parse)

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

setProxyType()

It determines the proxy type while allowing the request to be connected via proxy. Curl constants must be used.

$curl->setProxyType(CURLPROXY_HTTPS);

setProxyAuth()

It sets the proxy authentication information while allowing the request to connect via proxy.

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

or

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

or

$curl->setProxyAuth('user');

send()

It is the function where requests are made. It is used to implement request methods. All request methods in the class send requests using this method.

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

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

or

$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);

get()

Sends a request using the `GET` request method. This request does not accept the request body.

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

veya

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

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

veya

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

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

post()

It sends a request using the `POST` request method. This request accepts the request body. The setBody method is valid for all request methods that can be sent body.

$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);

put()

Throws a request using the `PUT` request method. This request accepts the request body. The setBody method is valid for all request methods that can be sent body.

$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);

put()

It sends a request using the `PUT` request method. This request accepts the request body. The setBody method is valid for all request methods that can be sent body.

$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);

delete()

It assigns a request using the `DELETE` request method. This request accepts the request body. The setBody method is valid for all request methods that can be sent body.

$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);

patch()

It assigns a request using the `PATCH` request method. This request accepts the request body. The setBody method is valid for all request methods that can send a body.

$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);

head()

It assigns a request using the `HEAD` request method. This request does not accept body and does not return a response.

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

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

connect()

It assigns a request using the `CONNECT` request method. This request does not accept the request body.

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

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

options()

It assigns a request using the `OPTIONS` request method. This request does not accept the request body.

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

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

trace()

It assigns a request using the `TRACE` request method. This request does not accept body and does not return a response.

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

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

exec()

Running this method is mandatory after setting up any request. The request is not sent until the `exec`` method is appended to the end of the request. This class works with the logic in the curl library. The reason for necessity is to make it easier to check the settings without sending the request.

$curl->exec();

> for setting check, print $curl variable with print_r method without adding exec.

Details of Response Methods

getInfo()

It is a method that returns detailed information about curl that occurs after sending a curl request.

All information can be directly returned as array type.

$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
)
*/

or it can also return singular information.

$curl->getInfo('http_code');

getCurlError()

If a curl-based error occurs after sending the request, this method returns the error.

$curl->getCurlError();

getResponse()

Returns the response from the request.

$curl->getResponse();

or you can return it by deleting the extra spaces in the answer.

$curl->getResponse(true);

getRespJson()

If the response received from the request is in json format, it returns the json data by parsing it. The return type is object. Array type can also be set.

$curl->getRespJson();

or return json data as array.

$curl->getRespJson(true);

Can set flag for `json_decode` with 2nd argument.

$curl->getRespJson(false, JSON_PRETTY_PRINT);

getEffective()

If the request has a redirect, it returns the last source visited. For this, the setReturn method must be set to true.

$curl->getEffective();

// https://httpbin.org/get

getHttpCode()

Returns the http status code of the request.

$curl->getHttpCode();

// 200

getHeader()

Returns any header returned from the request. Returns a singular value. The header id is added to the second argument. The header id argument returns data from the headers of the default last request.

$curl->getHeader('device_id');

// e324h4e708f097febb40384a51as48

or if there is a redirect, header can also be taken from the previous request. The header id is entered in the second argument.

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

// e324h4e708f097febb40384a51as48

getHeaders()

Returns all headers returned from the request. Array returns value. The header id is added to the first argument. The header id argument returns data from the headers of the default last request.

$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
)
*/

or

$curl->getHeaders(0);

getCookie()

Returns the specified cookies returned from the request response. The header id is valid in the 2nd argument.

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

// OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZMGYwMGEwOA==

or

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

// OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZMGYwMGEwOA==

getCookiesRaw()

Returns the cookies returned from the request in cookie format. header id is valid in 1st argument.

$curl->getCookiesRaw();

// laravel_session=eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=

or

$curl->getCookiesRaw(0);

// laravel_session=eyJpdiI6InV5bGRQNFJ4c01TYjZwT0I0amxzS1E9PSIsInZhbHVlIjoiZFI2WWpVWGxmTldDcVJvVlwvbVJicXBxM0pjRkVRUlBRKzZWb1BkbzliZHBVdTlmUEV4UzZkaFVMbmlRTHNYczFOZm5HSWkwRXhjb3BJRGI1NGRyM2tnPT0iLCJtYWMiOiJjMjAwMWIyMGIxYmQwYzkxMGQyNGJhMDZmZDJiNThjNGZhMTUyZWVjZDlkNjg5ZWVjYjY2MGE1ZTlmZDAxOGNmIn0=

getCookiesArray()

Returns the cookies returned from the request. The header id is valid in the 1st argument. As the name suggests, the returned data type is array.

$curl->getCookiesArray();

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

or

$curl->getCookiesArray(0);

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

getBetween()

Finds and returns text in the specified range from the response received from the request. Returns singular string data.


// <p>asd</p>

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

//asd

The 3rd argument is equal to the 1st argument of the `getResponse` method. It deletes the extra spaces in the response.

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

getBetweens()

Finds and returns text in the specified range from the response received from the request. Returns multiple array data. Gets all of the specified ranges if more than one.

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

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

The 3rd argument is equal to the 1st argument of the `getResponse`method. It deletes the extra spaces in the response.

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

Details of Other Methods

find()

It performs a text search in the response of the request, and returns an object type result if any. You can search for singular information as well as plural. The default source is the data retrieved from the `getResponse` method. Different source can be added to the optional 2nd argument.

title tag check

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

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

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

or multiple searches.

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

)
*/

or a different source can be added.

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

)
*/

getOpt()

Returns the values ??of constants set with the `setOpt` method. Names do not contain the names of constants. This is how the curl library offers.

$curl->getOpt();

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

or

$curl->getOpt(CURLOPT_URL);

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

Details of Constants

RAW

It allows it to detect the body data to be sent in the request as plain text.

$curl::RAW;

JSON

It makes it detect the body data to be sent in the request as json and converts it to json data.

$curl::JSON;

QUERY

It makes it detect the body data to be sent in the request as a form and converts it to form data.

$curl::QUERY;


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (1 directory)
Accessible without login Plain text file .gitignore Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Read me
Accessible without login Plain text file README.tr.md Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file Curl.php Class Class source
  Plain text file CurlException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageUnit (1 file)

  Files folder image Files  /  tests  /  Unit  
File Role Description
  Plain text file CurlTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:72
This week:1
All time:10,212
This week:560Up