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.
composer require 0x1881/curl
<?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');
- setDefault
- setMethod
- setUrl
- setHeader
- setBody
- setOpt
- setDebug
- setUserAgent
- setCookie
- setCookieFile
- setCookieJar
- setFollow
- setReturn
- setReferer
- setAutoReferer
- setTimeout
- setConnectTimeout
- setMaxConnect
- setMaxRedirect
- setProxy
- setProxyType
- setProxyAuth
- send
- get
- post
- put
- put
- delete
- patch
- head
- connect
- options
- trace
- exec
- getInfo
- getCurlError
- getResponse
- getRespJson
- getEffective
- getHttpCode
- getHeader
- getHeaders
- getCookie
- getCookiesRaw
- getCookiesArray
- getBetween
- getBetweens
Returns the class to its default request settings. It resets the settings to default on every request made.
$curl->setDefault();
Used to specify the request type.
GET
,POST
,PUT
,DELETE
,PATCH
,HEAD
,CONNECT
,OPTIONS
,TRACE
$curl->setMethod('GET');
Specifies the destination of the request.
$curl->setUrl('https://httpbin.org/get');
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);
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"}
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');
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);
Sets the User-Agent
header of the request.
$curl->setUserAgent('Googlebot/2.1 (+http://www.google.com/bot.html)');
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);
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');
It adds and saves the cookie information in Netscape format to the specified file.
$curl->setCookieJar(__DIR__.DIRECTORY_SEPARATOR.'cookies.txt');
Used to allow if the request is redirecting. The default false
is off.
$curl->setFollow(true);
Allows the request transfer to be output or not. The default false
is off.
$curl->setReturn(true);
Sets the Referer
header to use in the request.
$curl->setReferer('https://httpbin.org/');
If the request has redirected, it automatically sets the Referer
header to be used in the request. The default false
is off.
$curl->setAutoReferer(true);
Sets the timeout of curl functions in seconds.
$curl->setTimeout(5);
Sets the attempt time, in seconds, of the request.
$curl->setConnectTimeout(5);
Sets the maximum number of simultaneous connections.
$curl->setMaxConnect(5);
Sets the maximum number of redirects. The default value is 20
.
$curl->setMaxRedirect(5);
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');
It determines the proxy type while allowing the request to be connected via proxy. Curl constants must be used.
$curl->setProxyType(CURLPROXY_HTTPS);
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');
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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.
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');
If a curl-based error occurs after sending the request, this method returns the error.
$curl->getCurlError();
Returns the response from the request.
$curl->getResponse();
or you can return it by deleting the extra spaces in the answer.
$curl->getResponse(true);
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);
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
Returns the http status code of the request.
$curl->getHttpCode();
// 200
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
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);
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==
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=
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=
)
*/
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);
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
)
*/
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
)
)
*/
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/
It allows it to detect the body data to be sent in the request as plain text.
$curl::RAW;
It makes it detect the body data to be sent in the request as json and converts it to json data.
$curl::JSON;
It makes it detect the body data to be sent in the request as a form and converts it to form data.
$curl::QUERY;