Curl函数

Posted 钢锅

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Curl函数相关的知识,希望对你有一定的参考价值。

<?php
define(\'ROUTE_ROOT\',\'\');
define(\'ROUTE_PASSWORD\',\'\');
define(\'ROUTE_URL\',\'\');
define(\'COOKIE_FILE\',\'cookie_file.txt\');
define(\'TiYanShiJian\',\'30\');
define(\'ADMIN_PHONE\',\'0571-83055016\');
$time=time();
$ddate=$time+TiYanShiJian*60;
header("Content-Type:text/html;charset=UTF-8");
//require("config.php");
//封装登陆函数
function curl_tijiao($tijiao_url,$zhuce_data,$access,$callback){
    $cookie_file = dirname(__FILE__).\'/\'.COOKIE_FILE;
    $post_fields = \'user=\'.ROUTE_ROOT.\'&password=\'.ROUTE_PASSWORD;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $tijiao_url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[\'HTTP_USER_AGENT\']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if ($access==true){
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($zhuce_data));
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $return = curl_exec($ch);
    $tishi_json = iconv("GB2312","UTF-8",$return);
    //var_dump($zhuce_url);
    //var_dump($post_fields);
    curl_close($ch);
    if (strpos($tishi_json,\'{pppoe_client\') !== false) {
        //return $tishi_json;
        echo $callback."({ret:0,msg:\'亲,你别看!行么?\'})";
    }else if (strpos($tishi_json,\'{err\') !== false) {
        echo $callback.\'(\'.$tishi_json.\')\';
    }else{
        if ($access==true){
            $login_url = ROUTE_URL.\'login.cgi\';
            $ch = curl_init($login_url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
            curl_exec($ch);
            curl_close($ch);
        }
        //return error_msg(\'路由连接失败!\');
        echo $callback."({ret:1,msg:\'参数错误!或没有申请到key!支持Ajax跨域提交!详细QQ:264059790\'})";
    }
    //curl_close($ch);
}
//封装登陆函数结束
//开始获取参数

$callback = isset($_REQUEST[\'callback\']) ? trim($_REQUEST[\'callback\']) : \'\';
$Mob = isset($_REQUEST[\'Mob\']) ? trim($_REQUEST[\'Mob\']) : \'\';
$realname="zhouein";
$cellphone="0571-83055016";
$mem="beizhu>>".$realname.">>".$cellphone;
$tijiao_url=ROUTE_URL.\'/user.asp\';
$zhuce_data = array(
         \'user\' => \'15336500025\',
         \'pwd\' => \'15336500025\',
         \'ddate\' => \'1479138600\',
         \'mem\' => \'beizhu>>zhouein>>83055016\',
         \'num\' => \'1\',
         \'vlan\' => \'0\',
         \'state\' => \'96\',
         \'gid\' => \'0\',
         \'speed_mode\' => \'dl\',
         \'speed_up\' => \'20\',
         \'speed_down\' => \'500\',
         \'mod_pwd\' => \'0\',
         \'opt\' => \'add\',
         );
         
    $zhuce_data[\'user\']=$Mob;
    $zhuce_data[\'pwd\']=$Mob;
    $zhuce_data[\'mem\']=$mem;
    $zhuce_data[\'ddate\']=$ddate;
    $zhuce_data[\'_\']=$time;
    $return = curl_tijiao($tijiao_url,$zhuce_data,true,$callback);
?>
View Code

客户端

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    <title>Ajax Jsonp</title>  
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js"></script>  
    </head>  
    <script type="text/javascript">  
    $(function(){     
        $.ajax({  
            type : "GET",  
            async:false,  
            url : "http://www.zhouein.com/wayos_api/function.php",  
            dataType : "jsonp",
            data:{
                  Mob:"15967128951",
                  mykey:"222",
                  mystr:"ggg"
                  },
            jsonp: "callback",
            jsonpCallback:"zhouein",
            success : function(data){  
                $("#showcontent").text(data.msg)
                $("#showcontent").text(data.err)                
            },  
            error:function(){  
                alert(\'no!\');  
            }  
        });   
    });  
    </script>  
    <body>  
    <div id="showcontent">ddddddddddddddddddd:</div>
    <!--http://115.236.183.115:81/php_encode/?act=ENCODE&mykey=222&mystr=ggg&callback=kkkk-->
    </body>  
    </html>  

 

以上是关于Curl函数的主要内容,如果未能解决你的问题,请参考以下文章

CentOS yum 命令出现 [Errno 14] curl#6 - &quot;Couldn&#39;t resolve host ...&quot; 的解决方法(代码片段

有啥方法可以让 curl 连接并让我在输入时输入和发送身体片段?

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数

使用我的 curl 代码可以为目标网站生成多少带宽?

使用从循环内的代码片段中提取的函数避免代码冗余/计算开销