A simple PHP Curl class

Source: Internet
Author: User
Tags urlencode vars

I wrote a PHP5 based on curl, the function is relatively simple, more suitable for learning. If you really want to apply it, it is definitely recommended that Snoopy is too powerful. Although it is not on the basis of curl. Transfer from HTTP://XIAOWAI.GENTIEBA.COM/NODE/33

<?PHPclassMy_curl { Public function__construct () {!extension_loaded(' Curl ') &&Exit(' curl extension is not loaded, program terminates run ' ); Header(' content-type:text/html; Charset=utf-8 ' ); }          Public functionGo$url,$vars= ' ') {        $ch=Curl_init (); $array[Curlopt_url] =$url; $array[Curlopt_header] =false; $array[Curlopt_returntransfer] =true; $array[Curlopt_followlocation] =true; if(!Empty($vars )) {            $postfields=$this->render_postfields ($vars);//URL to generate Urlcode            $array[Curlopt_post] =true; $array[Curlopt_postfields] =$postfields; }                 //determine if there is a cookie, and some words are used directly        if($_cookie[' Cookie_jar '] | |Is_file($_cookie[' Cookie_jar '] )) {            $array[Curlopt_cookiefile] =$_cookie[' Cookie_jar '];//The cookie is judged here}Else {            $cookie _jar=Tempnam('/tmp ', ' Cookie ');//generate a cookie file            $array[Curlopt_cookiejar] =$cookie _jar;//Write Cookie Information            Setcookie(' Cookie_jar ',$cookie _jar);//Save Cookie Path} curl_setopt_array ($ch,$array);//Incoming Curl parameter        $content= Curl_exec ($ch);//ExecutionCurl_close ($ch);//Close        return $content;//return    }         protected functionRender_postfields ($vars) {        foreach($vars  as $key=$values ) {            $postdata.=UrlEncode($key) . "=" .UrlEncode($values) . "&"; }        return $postdata; }}?>
How to use
<?PHPif($_post[' Email ']&&$_post[' Password ']){    include' Curl_class.php ';$curl=NewMy_curl ();$vars[' email '] =$_post[' Email '];$vars[' password '] =$_post[' Password '];//$vars [' done '] = '/';//$vars [' submit '] = ' login ';Echo $curl->go (' Http://www.renren.com/PLogin.do ',$vars ); }Else{?>class= "Login-form" id= "LoginForm" method= "POST" > <input type= "text" id= "email" name= "email"/> <input Type= "text" id= "password" name= "password"/> <input type= "Submit" tabindex= "4" value= "Login"class= "Input-submit" id= "Login" > </form>

A simple PHP curl class

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.