CURL PHP Analog Browser get and post

Source: Internet
Author: User
Tags send cookies set cookie

A class that simulates a browser get and post data that needs to be used frequently,

Here are some good ways to collect.

Method One
<?phpdefine (' Is_proxy ', true); Whether to enable the proxy/* Cookie file */$cookie _file = dirname (__file__). "/cookie_". MD5 (basename (__file__)). ". txt"; Set cookie file save path and file name/* Mock browser */$user _agent = "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. NET CLR 1.1.4322) "; function Vlogin ($url, $data) {///impersonation Login Get Cookie function $curl = Curl_init ();//Start a curl if (is_proxy) {//The following code sets the proxy server//proxy address curl_setopt ($curl, Curlopt_proxy, $GLOBALS [' Proxy '])    ; } curl_setopt ($curl, Curlopt_url, $url); Address to access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($curl, curlopt_useragent, $GLOBALS [' user_agent ']); Simulates the browser @curl_setopt used by the user ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookiejar, $GLOBALS [' cookie_file ']); The name of the file holding the cookie information curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 30); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl);    Perform the action if (Curl_errno ($curl)) {echo ' errno '. Curl_error ($curl); } curl_close ($curl); Turn off the curl session return $tmpInfo;        Return Data}function Vget ($url) {///Impersonation get content function $curl = Curl_init ();//Start a Curl session if (Is_proxy) {//The following code sets the proxy server    Proxy server address curl_setopt ($curl, Curlopt_proxy, $GLOBALS [' Proxy ']); } curl_setopt ($curl, Curlopt_url, $url); Address to access curl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($CURL, Curlopt_useragent, $GLOBALS [' user_agent ']); Simulates the browser @curl_setopt used by the user ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_httpget, 1); Send a regular POST request curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 120); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl);    Perform the action if (Curl_errno ($curl)) {echo ' errno '. Curl_error ($curl); } curl_close ($curl); Turn off the curl session return $tmpInfo; Returns data}function vpost ($url, $data) {////Analog commit data function $curl = Curl_init ();//Start a Curl session if (Is_proxy) {//The following code is set    Proxy Server//proxy address curl_setopt ($curl, Curlopt_proxy, $GLOBALS [' Proxy ']); } curl_setopt ($curl, Curlopt_url, $url);    The address to be accessedcurl_setopt ($curl, Curlopt_ssl_verifypeer, 0); Inspection of the source of the certification Certificate curl_setopt ($curl, Curlopt_ssl_verifyhost, 1); Check that the SSL encryption algorithm exists from the certificate curl_setopt ($curl, curlopt_useragent, $GLOBALS [' user_agent ']); Simulates the browser @curl_setopt used by the user ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, Curlopt_autoreferer, 1); Auto set Referer curl_setopt ($curl, Curlopt_post, 1); Send a regular POST request curl_setopt ($curl, Curlopt_postfields, $data); Post-Submitted packet curl_setopt ($curl, Curlopt_cookiefile, $GLOBALS [' cookie_file ']); Read the cookie information stored above curl_setopt ($curl, Curlopt_timeout, 120); Set timeout limit to prevent dead loops curl_setopt ($curl, Curlopt_header, 0); Displays the contents of the Returned header area curl_setopt ($curl, Curlopt_returntransfer, 1); The information obtained is returned as a file stream $tmpInfo = curl_exec ($curl);    Perform the action if (Curl_errno ($curl)) {echo ' errno '. Curl_error ($curl); } curl_close ($curl); Key Curl session return $tmpInfo; Return Data}function Delcookie ($cookie _file) {//delete Cookie function unlink ($cookie _file); Perform delete}?>

Method Two
<?php/***file:curl.class.php*curl Package class, most operations of this class support chain Operation **example:** $curl =newcurl ($url); * $curl->exec (); *// Send post Data * $curl->post (Array (' username ' = ' username '))->exec (); */classcurl{private$ch; private$flag_if_have_ Run=false; Private$has_cloase=true; Publicfunction__construct ($url = ", $forgeIP =false) {$this->init ($url, $forgeIP);}/** * Initialize curl. If Curl is not turned off, first close the * * @paramtype $url * @return \common\library\curl */publicfunctioninit ($url = ", $forgeIP =false) {if (!$ This->has_cloase) {//If the last connection has not been completed, first turn off $this->close ();} if ($forgeIP) {//Forged IP, forged IP as the visitor's IP if (Validate:: Isipaddress ($forgeIP)) {$ip = $forgeIP;} else{$ip =$_server[' server_addr ';} $this->set_ip ($IP); } $this->ch=curl_init ($url); curl_setopt ($this->ch,curlopt_returntransfer,1); $this->has_cloase=false; Return$this; } publicfunctionseturl ($url) {curl_setopt ($this->ch,curlopt_url, $url); return$this;} publicfunctionclose () {if (! $this->has_close) {curl_close ($this->ch); $this->has_cloase=true;}} PublicfunCtion__destruct () {$this->close ();}/** * Set page timeout time, support chained operation * * @paramtype $timeout * @return \common\library\curl */ Publicfunctionset_time_out ($timeout) {curl_setopt ($this->ch,curlopt_timeout,intval ($timeout)); return$this;}/ * * Forged Source Path * * @paramtype $referer * @return \common\library\curl */publicfunctionset_referer ($referer) {if (!empty ($ Referer) {curl_setopt ($this->ch,curlopt_referer, $referer);} return$this; }/** * Set Cookies * This method only sends cookie information to the far end, does not save the cookie information returned by the far end * * @paramtype $cookie The storage path of the _file cookie file *@ Return\common\library\curl */Publicfunctionload_cookie ($cookie _file) {$this->_checkcookie ($cookie _file); curl_ Setopt ($this->ch,curlopt_cookiefile, $cookie _file); Return$this; }/** * Set cookies * Send cookies to the remote and save the remote return cookie * * @paramtype $cookie _file * @return \common\library\curl */ Publicfunctioncookie ($cookie _file) {$this->_checkcookie ($cookie _file); curl_setopt ($this->ch,curlopt_ Cookiefile, $cookie _file); curl_setopt ($this->ch,curlopt_cookiejar, $cookie _file); Return$this; }/** * Set Cookies * This method will not send cookie information, only receive the returned cookie and save * * @paramtype $cookie _file * @return \common\library\curl * * Publicfunctionsave_cookie ($cookie _file= "") {//Set cache file, for example A.txt if (empty ($cookie _file)) {$cookie _file=tempnam ('./', ' Cookies '); } $this->_checkcookie ($cookie _file); curl_setopt ($this->ch,curlopt_cookiejar, $cookie _file); Return$this; } privatefunction_checkcookie ($cookie _file) {if (!\think\storage::has ($cookie _file)) {\think\storage::p ut ($cookie _ File, '); }}/** * Perform curl Request * * @returntype */publicfunctionexec () {$str =curl_exec ($this->ch); $this->flag_if_have_run=true ; RETURN$STR; }/** * Set send POST request. There is no call to the method by default using the Get method to submit * * @paramtype $postData post data, supported arrays and "xxx=1&x=2" two formats * @return \common\library\curl */ Publicfunctionpost ($postData) {curl_setopt ($this->ch,curlopt_post,1);//echo ($postQuery);d ie; curl_setopt ($ This->ch,curlopt_postfields, $postData); Return$this; /** * Get Curl Info * * @returntype * @throwsException */publicfunctionget_info () {if ($this->flag_if_have_run==tRue) {Returncurl_getinfo ($this->ch);} else{thrownewexception ("

CURL PHP Analog Browser get and post

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.