Home  >  Article  >  Backend Development  >  The fifteenth day of php practice_PHP tutorial

The fifteenth day of php practice_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:12960browse

Learn curl again today


PHP provides a series of curl_* functions to operate curl.

The commonly used ones are as follows:
 Curl_init initializes a curl session
 Curl_close This is of course closed - -!
 Curl_error returns the error message of the current session
 Curl_errno error number
 Curl_setopt sets an option, this function is very important
 Curl_setopt_array This is the same as curl_setopt above, the difference is that this can set multiple options at one time
 Curl_exec executes curl session

These are the more commonly used ones, if you want to use Google for other functions.

Let’s simulate a GET request:

[php]
$ch = curl_init();//A session is initialized here
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');//Set an address related to the $ch session
curl_exec($ch);//Execute session
curl_close($ch);//Close session

$ch = curl_init();//A session is initialized here
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');//Set an address related to the $ch session
curl_exec($ch);//Execute session
curl_close($ch);//Close session
After browsing, the content of Google's homepage will be output on the browser.
If you ask, what if I don't want him to output, but return?
Then just add an option.


[php]
$ch = curl_init();//A session is initialized here
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');//Set an address related to the $ch session
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Tell curl here that I don’t want to output, I want to return
$data = curl_exec($ch);//Then curl will return to you during execution. Haha, so obedient..
curl_close($ch); //Close session

$ch = curl_init();//A session is initialized here
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com');//Set an address related to the $ch session
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Tell curl here that I don’t want to output and I want to return
$data = curl_exec($ch);//Then curl will return to you during execution. Haha, so obedient..
curl_close($ch); //Close the session. The above simulates a GET request, so the following simulates a POST:
[php] view plaincopyprint?//I used my own program to do experiments here. - -!
$url = 'http://www.phpfamily.cn/Shop/login?formaction=login';//POST address
$query = 'name=xiaokai&password=xiaokai';//This is the submitted data
$ch = curl_init($url);//Associate a url address during initialization
curl_setopt($ch, CURLOPT_POST, true); //Tell him here that I want to use the post method
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);//Give him the post data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//I don’t want to output, I want to return
$data = curl_exec($ch);//OK, it will be returned to you after execution.
curl_close($ch);//Close
echo $data;//Output the result, indicating that the login is successful.

//I used my own program for experiments here.. - -!
$url = 'http://www.phpfamily.cn/Shop/login?formaction=login';//POST address
$query = 'name=xiaokai&password=xiaokai';//This is the submitted data
$ch = curl_init($url);//Associate a url address during initialization
curl_setopt($ch, CURLOPT_POST, true);//Tell him here that I want to use the post method
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);//Give him the post data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//I don’t want output and want to return
$data = curl_exec($ch);//OK, it will be returned to you after execution.
curl_close($ch);//Close
echo $data;//Output the result, indicating that the login is successful.
Write the code yourself, don't copy, because you will never learn by copying.
After the above code is executed, it will prompt that the login is successful, then the login has been successful.
But there is a problem. If the login is not saved and refreshed, the login will be invalid. The problem is that your browser does not have cookies enabled
Same. So let's open cookies for curl.

[php]
$url = 'http://www.phpfamily.cn/Shop/login?formaction=login';//POST address
$query = 'name=xiaokai&password=xiaokai';//This is the submitted data
$jar = realpath('cookie.txt');//The address where the cookie is saved
$ch = curl_init($url);//Associate a url address during initialization
curl_setopt($ch, CURLOPT_POST, true); //Tell him here that I want to use the post method
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);//Give him the post data
curl_setopt($ch, CURLOPT_COOKIEJAR, $jar);//Give him the address where the cookie file is saved, and then the cookie will be automatically saved
The content is written to the cookie file.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//I don’t want to output, I want to return
$data = curl_exec($ch);//OK, it will be returned to you after execution
curl_close($ch);//Close
echo $data;//Output the result, indicating that the login is successful.

$url = 'http://www.phpfamily.cn/Shop/login?formaction=login';//POST address
$query = 'name=xiaokai&password=xiaokai';//This is the submitted data
$jar = realpath('cookie.txt');//The address where the cookie is saved
$ch = curl_init($url);//Associate a url address during initialization
curl_setopt($ch, CURLOPT_POST, true);//Tell him here that I want to use the post method
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);//Give him the post data
curl_setopt($ch, CURLOPT_COOKIEJAR, $jar);//Give him the address where the cookie file is saved, and then the cookie will be automatically saved
The content is written to the cookie file.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//I don’t want output and want to return
$data = curl_exec($ch);//OK, it will be returned to you after execution
curl_close($ch);//Close
echo $data;//Output the result, indicating that the login is successful.
Okay, just add a CURLOPT_COOKIEJAR option and it's OK. Isn't it very simple? Note the
here The value of CURLOPT_COOKIEJAR must be an absolute path, which means that the cookie file saving path you specify must be an absolute path.


[php]
$url = 'http://www.phpfamily.cn/Shop/register';
$jar = realpath('cookie.txt');//The address where the cookie is saved
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $jar);//Specify the path to save the cookie file here
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);//Close
echo $data;

$url = 'http://www.phpfamily.cn/Shop/register';
$jar = realpath('cookie.txt');//The address where the cookie is saved
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $jar);//Specify the path to save the cookie file here
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);//Close
echo $data;
Continue, the cookie is saved above, so there is no need to POST when simulating login again, add an option
CURLOPT_COOKIEFILE can log in directly.
Preview again and you will be prompted that you have logged in. In this way, after logging in, the cookie will be saved and then you can do anything, such as submitting
Comments, messages, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477350.htmlTechArticleToday we learn curl again. PHP provides a series of curl_* functions to operate curl. The following are commonly used: Curl_init initializes a curl session Curl_close This is of course closed...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn