PHP's cURL library provides an overview of grabbing web pages POST data and more

  • 2020-03-31 21:38:46
  • OfStack

Whether you want to take some data from a link, or take an XML file and import it into a database, or simply get the web content, the reaction kettle cURL is a powerful PHP library. This article focuses on how to use the PHP library.
Enable cURL Settings
First, we need to make sure that our PHP has the library open. You can get this information by using the php_info() function.
 
<?php 
phpinfo(); 
?> 

If you can see the following output on the page, the cURL library is turned on.
If you do, then you need to set up your PHP and open the library. If you're on a Windows platform, it's as simple as changing the Settings on your php.ini file, finding php_curl. DLL, and uncommenting the semicolon. As follows:
// uncomment the following
The extension = php_curl. DLL

If you're under Linux, you'll need to recompile your PHP. To edit, you'll need to turn on the compile parameter -- the configure command with the "waddle-curl" parameter.
A small example
If all goes well, here is a small routine:
 
<?php 
//Initializes an cURL object
$curl = curl_init(); 
//Set the URL you want to grab
curl_setopt($curl, CURLOPT_URL, 'http://jb51.net'); 
//Set the header
curl_setopt($curl, CURLOPT_HEADER, 1); 
//Set the cURL parameter to ask whether the result is saved to a string or output to the screen.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
//Run cURL, which requests a web page
$data = curl_exec($curl); 
//Close the URL request
curl_close($curl); 
//Displays the acquired data
var_dump($data); 

How to POST data
Above is the code to grab a web page, and below is the code to POST data to a web page. Suppose we have a deal with the form of http://www.example.com/sendSMS.php, its can accept two form fields, one is the phone number, one is the message content.
 
<?php 
$phoneNumber = '13912345678'; 
$message = 'This message was generated by curl and php'; 
$curlPost = 'pNUMBER=' . urlencode($phoneNumber) . '&MESSAGE=' . urlencode($message) . '&SUBMIT=Send'; 
$ch = curl_init();chain link fencing 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.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); 
?> 

From the above program, we can see that CURLOPT_POST is used to set the HTTP protocol's POST method instead of the GET method, and then CURLOPT_POSTFIELDS is used to set the POST data.
About proxy servers
The following is an example of how to use a proxy server. Notice the highlighted code, which is so simple that I don't have to say much more.
 
<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1); 
curl_setopt($ch, CURLOPT_PROXY, 'fakeproxy.com:1080'); 
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password'); 
$data = curl_exec(); 
curl_close($ch); 
?> 

About SSL and cookies
For the SSL protocol, which is HTTPS, the gas generator you just need to change the http:// in the CURLOPT_URL connection to https://. Of course, there is also a parameter called CURLOPT_SSL_VERIFYHOST that can be set to the verification site.
About cookies, you need to know the following three parameters:
CURLOPT_COOKIE, which sets a cookie in a face-to-face session
CURLOPT_COOKIEJAR, which saves a Cookie when the session ends
CURLOPT_COOKIEFILE, Cookie file.
HTTP server authentication
Finally, let's take a look at HTTP server authentication.
 
<?php 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt(CURLOPT_USERPWD, '[username]:[password]') 
$data = curl_exec(); 
curl_close($ch); 
?> 

For more, see the cURL manual.

Related articles: