问答详情
源自:6-3 curl的简单实例_x264

新人求指教原生php如何在浏览器中实现curl

<?php
header('Content-type:text');
//验证服务器地址的有效性
function check(){
    //1.将timestamp,nonce,token按字典序排序
        $timestamp = $_GET['timestamp'];
        $nonce = $_GET['nonce'];
        $token = "weixin";
        $signature = $_GET['signature'];
        $echostr = $_GET['echostr'];
//$_GET 变量用于收集来自 method="get" 的表单中的值。从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏),并且对发送的信息量也有限制(最多 100 个字符)
        $array = array( $timestamp,$nonce,$token );
//创建名为 $array 的索引数组,向它赋三个元素
        sort($array);
        //2.将排序后的三个参数进行拼接之后再用sha1加密
        $tmpstr = implode('',$array);
//把数组元素按指定规则组合为字符串
        $tmpstr = sha1( $tmpstr );
        //3.将加密后的字符串与signature进行对比,判断该请求是否来自微信
        if( $tmpstr == $signature && $echostr){
            //第一次接入微信api
            echo $echostr;
            exit;
        } else{
            //之后接入api即用户关注公众号和发送消息
            responseMsg();
        }
}
    check();
//接收事件推送并回复
function responseMsg(){
    
    //1.获取微信推送过来的数据,即post数据(为xml格式)
    $postArr = file_get_contents('php://input');
    //2.处理推送数据,并设置回复类型和内容
    /*推送XML数据包示例:

        <xml>
        <ToUserName><![CDATA[toUser]]></ToUserName>
        <FromUserName><![CDATA[FromUser]]></FromUserName>
        <CreateTime>123456789</CreateTime>
        <MsgType><![CDATA[event]]></MsgType>
        <Event><![CDATA[subscribe]]></Event>
        </xml> */
    $postObj = simplexml_load_string($postArr);//将xml数据转为对象
    //判断该数据包是否是订阅的事件推送
    if( strtolower( $postObj -> MsgType) == 'event'){
            //如果是关注公众号事件即subscribe则回复消息
             if(strtolower( $postObj -> Event) == 'subscribe'){
                  $toUser = $postObj -> FromUserName;
                 $fromUser = $postObj -> ToUserName;
                 $createTime = time();
                 $msgType = 'text';
                 $content ='您已成功关注平台:'.$postObj -> ToUserName;
                 $template = '  <xml>
                                <ToUserName><![CDATA[%s]]></ToUserName>
                                <FromUserName><![CDATA[%s]]></FromUserName>
                                <CreateTime>%s</CreateTime>
                                <MsgType><![CDATA[%s]]></MsgType>
                                <Content><![CDATA[%s]]></Content>
                                </xml>';
                 $info = sprintf( $template ,$toUser,$fromUser,$createTime,$msgType,$content);//将xml数据包进行对应的变量赋值
                  echo $info;
             }
        }
        //如果是用户发信息的事件则进行判断并回复消息
        if( strtolower($postObj->MsgType) == 'text'  && trim( $postObj -> Content) == "图文"){
            //回复图文消息
                $toUser = $postObj -> FromUserName;
             $fromUser = $postObj -> ToUserName;
             $createTime = time();
             $msgType = "news";
             $arr =array(
                 array(
                    'title'=>"百度",
                    'description'=>"百度一下,你就知道",
                    'url'=>"http://www.baidu.com",
                    'picUrl'=>"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"
                ),
                array(
                    'title'=>"慕课",
                    'description'=>"我爱学习",
                    'url'=>"http://www.imooc.com",
                    'picUrl'=>"http://www.imooc.com/static/img/common/logo.png"
                ),
                array(
                    'title'=>"淘宝",
                    'description'=>"淘,你喜欢",
                    'url'=>"http://www.taobao.com",
                    'picUrl'=>"//www.taobao.com?spm=a21bo.50862.201857.1.iMrzPE"
                )
                
             );//图文信息
             $template="<xml>
                        <ToUserName><![CDATA[%s]]></ToUserName>
                        <FromUserName><![CDATA[%s]]></FromUserName>
                        <CreateTime>%s</CreateTime>
                        <MsgType><![CDATA[%s]]></MsgType>
                        <ArticleCount>".count($arr)."</ArticleCount>
                        <Articles>";
             foreach( $arr as $k => $v){
                  $template .= " <item>
                        <Title><![CDATA[".$v['title']."]]></Title>
                        <Description><![CDATA[".$v['descriptition']."]]></Description>
                        <PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
                        <Url><![CDATA[".$v['url']."]]></Url>
                        </item>
                      ";    
             }
              $template .= "</Articles>
                        </xml>";
              echo sprintf($template, $toUser, $fromUser, $createTime, $msgType);
                    //注意:进行多图文发送时,子图文个数不能超过10个
        }else{
             //回复文本消息
            switch(trim( $postObj -> Content)){
                case 1:
                    $content = "你输入了1";
                    break;
                case 2:
                    $content = "你输入了2";
                    break;
                case 3:
                    $content = "你输入了3";
                    break;
                case "百度一下":
                    $content = "<a href='http://www.baidu.com'>百度</a>";
                    break;
            
            }
                    $toUser = $postObj->FromUserName;
                    $fromUser = $postObj->ToUserName;
                       $createTime = time();
                    $msgType = 'text';
                    $template = '  <xml>
                                <ToUserName><![CDATA[%s]]></ToUserName>
                                <FromUserName><![CDATA[%s]]></FromUserName>
                                <CreateTime>%s</CreateTime>
                                <MsgType><![CDATA[%s]]></MsgType>
                                <Content><![CDATA[%s]]></Content>
                                </xml>';
                    echo sprintf($template, $toUser, $fromUser, $createTime, $msgType,$content);
        }
    }

     function http_curl(){
          //通过php的curl工具采集某个url的内容
        //1.初始化curl
        $ch = curl_init();
        $url = "http://www.baidu.com";
        //2.设置请求选项, 包括具体的url
        curl_setopt($ch,CURLOPT_URL,$url);//需要获取的URL地址,也可以在curl_init()函数中设置
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
        //3.采集内容
        $response = curl_exec($ch);//执行一个cURL会话
        //4.关闭CURL会话
        curl_close($ch);
        var_dump( $response );//判断一个变量的类型与长度,并输出变量的数值
    }

?>


http://img.mukewang.com/58ee00150001d3b907210242.jpg

提问者:qq_T_T若是人间四月天_0 2017-04-12 18:23

个回答

  • 妙柴
    2017-04-12 18:47:25

    首先要你的PHP环境开启支持curl才成