Curl is used for the work in the past two days. when the requested url is not on the same server, it cannot be accessed directly. in this case, it is best to use curl, simulate post requests to do something,
Curl is used for the work in the past two days. when the requested url is not on the same server, it cannot be accessed directly. in this case, it is best to use curl, it is simple and convenient to simulate post requests. The following describes the accumulation of post requests during usage. the instance code is as follows:
- /*
- Remote post request
- */
- Function getRemoteUrl ($ get_url ){
- $ Curl = curl_init (); # Start a CURL session
- Curl_setopt ($ curl, CURLOPT_URL, $ get_url); # set a Url
- Curl_setopt ($ curl, CURLOPT_POST, true); # send a regular Post request
- Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); # set the timeout limit to prevent endless loops
- Curl_setopt ($ curl, CURLOPT_HEADER, 0); # display the returned Header area content
- Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); # The obtained information is returned as a file stream
- $ Return = curl_exec ($ curl); # perform the operation
- If (curl_errno ($ curl )){
- Return false;
- }
- Curl_close ($ curl); # Close a CURL session
- Return $ return;
- }
- Function getCurlData ($ url ){
- $ Curl = curl_init ();
- Curl_setopt ($ curl, CURLOPT_URL, $ url );
- Curl_setopt ($ curl, CURLOPT_HEADER, false );
- Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
- Curl_setopt ($ curl, CURLOPT_TIMEOUT, 10 );
- $ Result = curl_exec ($ curl );
- Curl_close ($ curl );
- Return $ result;
- }
To sum up the curl method:
Initialize curl first, and use curl_setopt to set the target url and other options.
Curl_exec: execute curl
Disable curl
The last step is output.
Curl () is highly efficient and supports multiple threads. however, to enable curl extension, follow these steps:
1. copy the three php_curl.dll, libeay32.dll, and ssleay32.dll files in the PHP folder to system32;
2. remove the semicolon from php. ini (c: WINDOWS directory) and extension = php_curl.dll;
3. restart apache or IIS