Heim php教程 PHP源码 一个简单的PHP的CURL类

一个简单的PHP的CURL类

May 25, 2016 pm 05:15 PM

我写了一个基于CURL的PHP5的类,功能比较简单,比较适合学习用。如果真要应用的话,那绝对推荐Snoopy 太强大了。虽然它不是在curl基础上的。

php代码

<?php
class my_curl {
     
    public function __construct() {
        ! extension_loaded ( &#39;curl&#39; ) && exit ( &#39;CURL扩展未加载,程序终止运行&#39; );
        header ( &#39;Content-Type: text/html; charset=utf-8&#39; );
    }
     
    public function go($url, $vars = &#39;&#39;) {
        $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 [&#39;cookie_jar&#39;] || is_file ( $_COOKIE [&#39;cookie_jar&#39;] )) {
            $array [CURLOPT_COOKIEFILE] = $_COOKIE [&#39;cookie_jar&#39;]; //这里判断cookie
        } else {
            $cookie_jar = tempnam ( &#39;/tmp&#39;, &#39;cookie&#39; ); //产生一个cookie文件
            $array [CURLOPT_COOKIEJAR] = $cookie_jar; //写入cookie信息
            setcookie ( &#39;cookie_jar&#39;, $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;
    }
}
?>
Nach dem Login kopieren

使用方法

<?php
if($_POST[&#39;email&#39;]&&$_POST[&#39;password&#39;]){
    include &#39;curl_class.php&#39;;
$curl = new my_curl ();
$vars [&#39;email&#39;] = $_POST[&#39;email&#39;];
$vars [&#39;password&#39;] = $_POST[&#39;password&#39;];
//$vars [&#39;done&#39;] = &#39;/&#39;;
//$vars [&#39;submit&#39;] = &#39;登 录&#39;;
echo $curl->go ( &#39;http://www.renren.com/PLogin.do&#39;, $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 }?>
Nach dem Login kopieren
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)