Similar to dreamhost such as host service providers, is to display the use of fopen. Use of PHP's curl can be implemented to support FTP, FTPS, HTTP htpps SCP SFTP TFTP, TELNET DICT file and LDAP. Curl supports SSL certificates, HTTP POST, http PUT, FTP uploads, Kerberos, HTT-based uploads, proxies, cookies, user + password proofs, File transfer recovery, HTTP proxy channels are most commonly used, HTTP-based Get and Post methods.
Code implementation:
1. Get implementation of HTTP
- $ch = Curl_init ("http://www.domain.com/api/index.php?test=1");
- curl_setopt ($ch, Curlopt_returntransfer, true); //Get Data back
- curl_setopt ($ch, Curlopt_binarytransfer, true); //Get Data back when Curlopt_returntransfer is enabled
- echo $output = curl_exec ($ch);
- /* Write file */
- $fh = fopen ("out.html", ' W ');
- Fwrite ($fh, $output);
- Fclose ($fh);
2, the HTTP POST implementation
PHP code
- <?php
- $url = ' http://www.domain.com/api/';
- $fields = Array (
- ' lname ' = 'justcoding ',
- ' fname ' = 'phplover ',
- ' title ' = 'myapi ',
- ' age ' = ',
- ' email ' = '[email protected] ',
- ' phone ' = '1353777303 '
- );
- $post _data = Implode (' & ', $fields);
- 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 regular post request is sent with the type: application/x-www-form-urlencoded, just like the form submitted.
- curl_setopt ($ch, Curlopt_postfields,$fields); //"POST" operation in HTTP. If you want to transfer a file, you need a filename at the start of the @
- Ob_start ();
- Curl_exec ($ch);
- $result = Ob_get_contents ();
- Ob_end_clean ();
- echo $result;
- Close connection
- Curl_close ($ch);
http://www.domain.com/api/index.php
PHP code
- <?php
- if ($_get[' test ')
- {
- Print_r ($_get);
- }
- if ($_post)
- {
- Print_r ($_post);
- }
3. PHP's Curl Transfer cookie
Two different ways:
One is automatic:
PHP code
- curl_setopt ($curlHandle, Curlopt_cookiejar, ' cookie.txt '); //Save
- curl_setopt ($curlHandle, Curlopt_cookiefile, ' cookie.txt '); //Read
This will automatically keep the cookie up.
But two times, one is to first access the cookie, then the link to use the cookie
Example:
PHP code
- <?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 cookie path for file read and submit
- 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); //See here, you can also say you come from Google
- curl_setopt ($ch, curlopt_useragent, "www.domain.com");
- //$request = "jsessionid=abc6szw15ozvz_pu9b-8r";//Set Post parameters
- //curl_setopt ($ch, Curlopt_postfields, $request);
- //Above this sentence, of course you can say you are Baidu, get rid of the value here OK, can realize the function of thieves, $_server[' http_user_agent ']
- //You can also make yourself a spider, so disguise the curlopt_useragent here .
- //If you want to put this program on Linux with Php-q execution that also write specific $_server[' http_user_agent ', forged can also
- curl_setopt ($ch, Curlopt_returntransfer, 1);
- curl_setopt ($ch, Curlopt_cookiefile, $cookie _jar);
- curl_setopt ($ch, Curlopt_header, false); Set whether to output 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;
- }
- ?>
A custom:
PHP code
- $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);