php curl_init function usage

  • 2020-12-19 20:57:12
  • OfStack

Whether you want to pull some data from a link or take an XML file and import it into a database, cURL is a powerful PHP library.

CURL function library in PHP (Client URL Library Function)

curl_close - Closes 1 curl session
curl_copy_handle - Copies all contents and parameters of 1 curl connection resource
curl_errno - Returns 1 numeric number containing the current session error message
curl_error - Returns 1 string containing the current session error message
curl_exec - Perform 1 curl session
curl_getinfo - Gets information for 1 curl connection resource handle
curl_init - Initializes 1 curl session
curl_multi_add_handle - Adds a separate curl handle resource to the curl batch session
curl_multi_close - Closes 1 batch handle resource
curl_multi_exec - Resolves 1 curl batch handle
curl_multi_getcontent - returns the text stream of the retrieved output
curl_multi_info_read - Gets the relevant transport information for the currently parsed curl
curl_multi_init - Initializes 1 curl batch handle resource
curl_multi_remove_handle - Removes a handle resource from the curl batch handle resource
curl_multi_select -- Get all the sockets with cURL extension, which can be "selected"
curl_setopt_array - Sets the session parameter as an array for 1 curl
curl_setopt - Sets the session parameter for 1 curl
curl_version - Gets es107EN-related version information

The curl_init() function initializes an curl session. The curl_init() function has only 1 argument that is optional and represents an url address.
The curl_exec() function executes an curl session, with only one argument being the handle returned by the curl_init() function.
The curl_close() function closes an curl session, only one taking a handle returned by the curl_init() function.

Example 1: A basic example
The basic example


<?php
//  Initialize the 1 a  cURL  object 
$curl = curl_init();
//  Set what you need to grab URL
curl_setopt($curl, CURLOPT_URL, 'http://www.cmx8.cn');
//  Set up the header
curl_setopt($curl, CURLOPT_HEADER, 1);
//  Set up the cURL  Argument that requires the result to be saved in a string or output to the screen. 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//  run cURL , request page 
$data = curl_exec($curl);
//  Shut down URL request 
curl_close($curl);
//  Displays the obtained data 
var_dump($data);
?>

Example 2: POST data

sendSMS. php, which accepts two form fields, one for a phone number and one for SMS content.
POST data


<?php
$phoneNumber = '13812345678';
$message = 'This message was generated by curl and php';
$curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.lxvoip.com/sendSMS.php');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec();
curl_close($ch);
?>

Example 3: Using a proxy server
Using a proxy server


<?php 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.cmx8.cn');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, 'proxy.lxvoip.com:1080');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');
$data = curl_exec();
curl_close($ch);
?>

Example 4: Simulated login

Curl simulation login discuz program, suitable for DZ7.0, username to your user name,userpass to your password is ok.
Curl simulates logging in to the discuz program


<?php   

!extension_loaded('curl') && die('The curl extension is not loaded.');   

$discuz_url = 'http://www.lxvoip.com';// BBS address    
$login_url = $discuz_url .'/logging.php?action=login';// Login page address    
$get_url = $discuz_url .'/my.php?item=threads'; // My post    

$post_fields = array();   
// The following two items need not be modified    
$post_fields['loginfield'] = 'username';   
$post_fields['loginsubmit'] = 'true';   
// Username and password must be filled in    
$post_fields['username'] = 'lxvoip';   
$post_fields['password'] = '88888888';   
// Security question    
$post_fields['questionid'] = 0;   
$post_fields['answer'] = '';   
//@todo Verification code    
$post_fields['seccodeverify'] = '';   

// Get the form FORMHASH   
$ch = curl_init($login_url);   
curl_setopt($ch, CURLOPT_HEADER, 0);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
$contents = curl_exec($ch);   
curl_close($ch);   
preg_match('/<input\s*type="hidden"\s*name="formhash"\s*value="(.*?)"\s*\/>/i', $contents, $matches);   
if(!empty($matches)) {   
    $formhash = $matches[1];   
} else {   
    die('Not found the forumhash.');   
}   

//POST Data, acquisition COOKIE   
$cookie_file = dirname(__FILE__) . '/cookie.txt';   
//$cookie_file = tempnam('/tmp');   
$ch = curl_init($login_url);   
curl_setopt($ch, CURLOPT_HEADER, 0);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
curl_setopt($ch, CURLOPT_POST, 1);   
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);   
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);   
curl_exec($ch);   
curl_close($ch);   

// Take what I got up here COOKIE Get the content of the page that you need to log in to view    
$ch = curl_init($get_url);   
curl_setopt($ch, CURLOPT_HEADER, 0);   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);   
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);   
$contents = curl_exec($ch);   
curl_close($ch);   

var_dump($contents); 


Related articles: