我写了一个基于CURL的PHP5的类,功能比较简单,比较适合学习用。如果真要应用的话,那绝对推荐Snoopy 太强大了。虽然它不是在curl基础上的。
php代码
<?php class my_curl { public function __construct() { ! extension_loaded ( 'curl' ) && exit ( 'CURL扩展未加载,程序终止运行' ); header ( 'Content-Type: text/html; charset=utf-8' ); } public function go($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 ); //生成urlcode的url $array [CURLOPT_POST] = true; $array [CURLOPT_POSTFIELDS] = $postfields; } //判断是否有cookie,有的话直接使用 if ($_COOKIE ['cookie_jar'] || is_file ( $_COOKIE ['cookie_jar'] )) { $array [CURLOPT_COOKIEFILE] = $_COOKIE ['cookie_jar']; //这里判断cookie } else { $cookie_jar = tempnam ( '/tmp', 'cookie' ); //产生一个cookie文件 $array [CURLOPT_COOKIEJAR] = $cookie_jar; //写入cookie信息 setcookie ( 'cookie_jar', $cookie_jar ); //保存cookie路径 } curl_setopt_array ( $ch, $array ); //传入curl参数 $content = curl_exec ( $ch ); //执行 curl_close ( $ch );//关闭 return $content; //返回 } protected function render_postfields($vars) { foreach ( $vars as $key => $values ) { $postdata .= urlencode ( $key ) . "=" . urlencode ( $values ) . "&"; } return $postdata; } } ?>
使用方法
<?php if($_POST['email']&&$_POST['password']){ include 'curl_class.php'; $curl = new my_curl (); $vars ['email'] = $_POST['email']; $vars ['password'] = $_POST['password']; //$vars ['done'] = '/'; //$vars ['submit'] = '登 录'; echo $curl->go ( 'http://www.renren.com/PLogin.do', $vars ); }else{ ?> <html> <form action="" 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="登录" class="input-submit" id="login"> </form> </html> <?php }?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
3 weeks agoByDDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
3 weeks agoByDDD

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
