in , ,

PHP Lib cURL workshop and action tips

PHP Lib cURL workshop and action tips

This is a Full parted example of PHP cURL library libcurl.

Initiate cURL instance and set Request method

The default cURL request method is GET request, so URL Parameters can be inclusiding normally into the URL: https://example.com/?q=value&u=value2. but to set cURL option using  `curl_setopt` method to use POST request..

for example we need to submit a login Form using cURL.

// Start new instance of cURL library.
$curl = curl_init();

// Set request to POST type and set the request parameters
$postvars= 'passwdfieldname=password&usernamefieldname=username';

curl_setopt($curl , CURLOPT_POST, true);
curl_setopt($curl , CURLOPT_POSTFIELDS, $postvars);

// Handle and set COOKIES
// you can set a full path for cookies file.
curl_setopt ($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
// Send the COOKIES 
curl_setopt ($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
// Follow location after the POST Submitted
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

Set cURL USER-AGENT target a URL

// Set URL to POST form, Login form or any.
curl_setopt($curl, CURLOPT_URL, 'https://www.example.com/Login.php');
// Set USER-AGENT
$agentstring = "Mozilla/5.0 (iPhone; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1";

curl_setopt($curl, CURLOPT_USERAGENT, $agentstring);

Skip cURL SSL Verifications

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

cURL Authontication

// Set your login and password for authentication
curl_setopt($curl, CURLOPT_USERPWD, 'login:pasword');

// You can use CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE,
// CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE
//
// You can use the bitwise | (or) operator to combine more than one method.
// If you do this, CURL will poll the server to see what methods it supports and pick the best one.
//
// CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST |
// CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM
//
// CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE |
// CURLAUTH_NTLM
//
// CURLAUTH_ANY is prefered as it covers all bases
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// this example source found in this url

Tunning Connection

// Use Proxy.
curl_setopt($curl, CURLOPT_PROXY, '192.168.1.10:8080');
// Set timeout on connect in sec.
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
// Set timeout on response in sec. 
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
// Return transfer output in a string format.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Follow this amount of redirects
curl_setopt($curl, CURLOPT_MAXREDIRS, 3); 
// Set The referer URL
curl_setopt($curl, CURLOPT_REFERER,'http://example.com/aboutme.html');
// Set Auto referer.
curl_setopt($curl,CURLOPT_AUTOREFERER,true);

Send request,Get output, and Close the Connection

// Send the request & save response to $response
$response = curl_exec($curl);
// Continue transfer if needed ie.. Download a file, and execute the second request.
curl_setopt($curl, CURLOPT_URL, 'https://www.example.com/file.pdf');
$content = curl_exec ($curl);
// Close connection and cURL session
curl_close($curl);

Read more for curl_setopt at https://curl.haxx.se/libcurl/c/curl_easy_setopt.html

Hope that helps some bodies.
Thanks.

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

      Optimizing Apache2

      Optimizing Apache2

      Login with PHP and cURL

      Login with PHP and cURL