What’s special about this book is that it provides everyone with the core ideas of important operations. We take real steps in database, image, and file uploading. It also explains how the steps are organized into our code and implementation process.
We divide the steps of curl into the following 7 steps:
1. Initialize the curl resource
2. Parameter setting request protocol address
3. Whether the parameter setting returns the request result
4. Set the sending data (no need to set if there is no sending data)
5. Other parameter information settings (according to actual work needs Decision)
6. Execute or execute to get the return result
7. Close the curl resource
In order to better let everyone learn and get started, we will change steps 2 to 2. The 5 steps that should have been classified as one step were inserted into 4 steps.
The second to fifth steps should essentially be unified into curl parameter settings.
All curl setting parameter settings are completed through curl_setopt
curl_setopt This step is the most important, and all the secrets are here.
curl_setopt This step completes the detailed settings including connection, parameters and all requests. It can be difficult to read and understand them all at once, so we'll just try the more common and useful options.
We are here for a better introduction. And what we use most in php is http request. Therefore, we will not explain all the things that you do not need to use.
If you are interested, you can refer to the complex parameter settings in curl_setopt.
The address is as follows: http://php.net/manual/zh/function.curl-setopt.php
1. Initial use of curl resources
There is only one sentence, that is, the curl_init function is used. This parameter method requires any parameters to be passed in. Returns the curl operation resource.
Because, we later push data into curl's operating resource variables through curl_setopt.
Example:
$ch = curl_init();
2. Parameter setting request protocol address
The detailed usage of the curl_setopt function is as follows:
Type | Description |
---|
Function | curl_setopt |
##Parameter 1 | curl resource variable |
Parameter 2 | curl parameter option |
Parameter 3 | curl parameter value |
CURLOPT_URL This parameter option specifies the requested url address.
curl_setopt($ch, CURLOPT_URL, "http://www.php.cn");
3. Whether the parameter setting returns the request result
We hope that the curl request will return the corresponding result. If we want to get the corresponding result, we also need to set a parameter. This parameter is called: CURLOPT_RETURNTRANSFER.
If required, the return value is 1. The result returned after no request can be set to 0.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
4. Set the sending data
If it is a get request, we do not need to set the sending parameters. When posting and waiting for requests, we need to set the sending method to the post method. and set the data to be sent.
*CURLOPT_POST *Set the value to 1 to use the POST method, and 0 to not use the POST method
CURLOPT_POSTFIELDSSet the passed data
//声明使用POST方式来进行发送
curl_setopt($ch, CURLOPT_POST, 1);
//发送什么数据呢
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
5. Other parameter information settings
If it is https, sometimes we need to ignore the security certificate of https.
CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST are changed to false to ignore the certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
CURLOPT_HEADER This parameter also determines whether to process http header information. If we do not want to receive processing, we can set this value to 0.
curl_setopt($ch, CURLOPT_HEADER, 0);
In addition, we can also set the timeout of the request, the parameter is: CURLOPT_TIMEOUT.
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
There are many other parameters. We have talked about it before. If you want to know more, you can visit:
The address is as follows: http://php.net/manual/zh/function.curl -setopt.php
6. Execute or execute to get the return result
In our third step, set the value of the CURLOPT_RETURNTRANSFER parameter to 1. If the execution result has data. After execution using curl_exec, the result will be returned to the $output variable.
$output = curl_exec($ch);
7. Close curl resources
Close curl resources. Because of the resource type, we have repeatedly emphasized that if it is opened, it will be closed.
If you don’t need to use it, use curl_close to close it and release the memory immediately.
curl_close($ch);
8. Put the above together
<?php
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "http://www.php.cn");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
print_r($output);
?>
Next Section<?php
//初始化
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "http://www.php.cn");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
//释放curl句柄
curl_close($ch);
//打印获得的数据
print_r($output);
?>
- Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...