Similar to host service providers such as dreamhost, it shows the use of fopen. Php curl supports FTP, FTPS, http htpps scp sftp tftp telnet dict file and LDAP. Curl supports SSL certificates, http post, http put, and FTP uploads, kerberos, HTT-based upload, proxy, cookie, user + password proof, file transfer recovery, and http Proxy channel are the most common methods based on http get and post.
Code implementation:
1. http get implementation
Copy codeThe Code is as follows:
$ Ch = curl_init ("http://www.domain.com/api/index.php? Test = 1 ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return
Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true); // when CURLOPT_RETURNTRANSFER is enabled, data is returned.
Echo $ output = curl_exec ($ ch );
/* Write File */
$ Fh = fopen ("out.html", 'w ');
Fwrite ($ fh, $ output );
Fclose ($ fh );
2. http post implementation
Copy codeThe Code is as follows:
<? Php
$ Url = 'HTTP: // www.domain.com/api /';
$ Fields = array (
'Lname' => 'justencoding ',
'Fname' => 'phplover ',
'Title' => 'myapi ',
'Age' => '27 ',
'Email '=> '2017 @ gmail.com ',
'Phone' => '123'
);
// $ Post_data = implode ('&', $ fields );
Note:The parameters of the post request must be connected in the get mode and passed as strings:
For example:$ Params = 'userid = '. $ this-> user_id.' & auth = '. $ this-> auth.' & sig = '. $ this-> sig
There are also cross-platform requests, curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect (important)
// Open connection
$ Ch = curl_init ();
// Set the url, number of POST vars, POST data
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, count ($ fields); // When enabled, a conventional POST request is sent. The type is application/x-www-form-urlencoded, just like submitting a form.
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields); // the "POST" Operation in HTTP. If you want to transfer a file, you need a file name starting @.
Ob_start ();
Curl_exec ($ ch );
$ Result = ob_get_contents ();
Ob_end_clean ();
Echo $ result;
// Close connection
Curl_close ($ ch );
Copy codeThe Code is as follows:
<? Php
If ($ _ GET ['test'])
{
Print_r ($ _ GET );
}
If ($ _ POST)
{
Print_r ($ _ POST );
}
Php curl transfer cookie
Two methods:
One is automatic:
Copy codeThe Code is as follows:
Curl_setopt ($ curlHandle, CURLOPT_COOKIEJAR, 'cookie.txt '); // save
Curl_setopt ($ curlHandle, CURLOPT_COOKIEFILE, 'cookie.txt '); // read
In this way, the COOKIE will be automatically followed up.
However, it takes two times. One is to generate a cookie first, and then connect it to use the cookie.
Example:
Copy codeThe Code is as follows:
<? Php
Function get_curlcuconent2 ($ filename, $ referer)
{
$ Cookie_jar = tempnam ('./tmp', 'jsessionid ');
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ filename );
Curl_setopt ($ ch, CURLOPT_HEADER, false );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// Set the cookie Path for file reading and submission
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_jar );
$ Filecontent = curl_exec ($ ch );
Curl_close ($ ch );
$ Ch = curl_init ();
$ Hostname = "www.domain.com ";
// $ Referer = "http://www.domain.com /";
Curl_setopt ($ ch, CURLOPT_URL, $ filename );
Curl_setopt ($ ch, CURLOPT_REFERER, $ referer); // You can also say that you are from google
Curl_setopt ($ ch, CURLOPT_USERAGENT, "www.domain.com ");
// $ Request = "JSESSIONID = abc6szw15ozvZ_PU9b-8r"; // set the POST Parameter
// Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ request );
// The above sentence, of course, you can say that you are a baidu. If you get rid of the value here, it will be OK. You can implement the thief function, $ _ SERVER ['HTTP _ USER_AGENT ']
// You can also create a spider by yourself, so pretend to be the CURLOPT_USERAGENT here.
// If you want to put this program on linux and execute it using php-q, you should also write the specific $ _ SERVER ['HTTP _ USER_AGENT ']. Forged programs can also be used.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_jar );
Curl_setopt ($ ch, CURLOPT_HEADER, false); // you can specify whether to output the page content.
Curl_setopt ($ ch, CURLOPT_GET, 1); // post, get past
$ Filecontent = curl_exec ($ ch );
Preg_match_all ("/charset = (. + ?) [NULL \ "\ ']/is", $ filecontent, $ charsetarray );
If (strtolower ($ charsetarray [1] [0]) = "UTF-8 ")
$ Filecontent = iconv ('utf-8', 'gb18030 // IGNORE ', $ filecontent );
Curl_close ($ ch );
Return $ filecontent;
}
?>
Custom:
Copy codeThe Code is as follows:
$ Header [] = 'Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, *'. '/*';
$ Header [] = 'Accept-Language: zh-cn ';
$ Header [] = 'user-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;. net clr 2.0.50727 )';
$ Header [] = 'host: '. $ your target Host;
$ Header [] = 'Connection: Keep-Alive ';
$ Header [] = 'cookie: '. $ your Cookie string;
Curl_setopt ($ curlHandel, CURLOPT_HTTPHEADER, $ header );