首页 课程中心 问答&分享 读书 公众号框架 HUI H.JS 开放源 登录 & 注册
 《微信公众号开发权威教程》

cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP、FTP、TELNET等。常用有接口通信、数据采集等。在微信开发接口通信过程中会经常用到。


开启php的curl扩展

extension=php_curl.dll

window环境下: 将PHP文件夹下的三个文件php_curl.dll,libeay32.dll,ssleay32.dll复制到system32下;

检查curl是否开启

<?php
$curl = curl_init();
var_dump($curl); //开启成功: resource(2) of type (curl)


相关资料:《php CURL 详解》

http://www.hcoder.net/course/info_196.html


将curl封装到微信核心类

在核心类 weChat中增加成员变量:public $curlStatus; //curl状态信息

编写2个函数

//curl get 方式 封装
public function curlGet($url){
    $ch   = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
    curl_setopt($ch, CURLOPT_ENCODING       , 'gzip,deflate');
    $res  = curl_exec($ch);
    $this->curlStatus = curl_getinfo($ch);
    curl_close($ch);
    return $res;
}
/*
 * curl POST 方式
 * 参数1 $url
 * 参数2 $data 格式 array('name'=>'test', 'age' => 18)
 */
public function curlPost($url, $data){
    $ch   = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_ENCODING       , 'gzip,deflate');
    $res  = curl_exec($ch);
    $this->curlStatus = curl_getinfo($ch);
    curl_close($ch);
    return $res;
}


完整代码及效果监测

<?php
define("TOKEN"    , "wxtest2016");
$wechatObj = new weChat();
//curl post方式测试
echo $wechatObj->curlPost('http://wx.hcoder.net', array('name'=>'test', 'age' => 18));

//核心类
class weChat{
    public $postObj;      //接收到的xml对象
    public $openId;       //客户的openId
    public $ourOpenId;    //我方公众号的openId
    public $msgType;      //客户消息的类型
    public $curlStatus;   //curl状态信息
    public $accessTokenFile = 'accessToken.txt'; //accessToken文件地址
    //构造函数用于接收消息
    public function __construct(){
        if(!empty($GLOBALS["HTTP_RAW_POST_DATA"])){
	//将xml转换成对象
	libxml_disable_entity_loader(true);
	$this->postObj      = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
	$this->openId       = $this->postObj->FromUserName;
	$this->ourOpenId    = $this->postObj->ToUserName;
	$this->msgType      = $this->postObj->MsgType;
	}
    }
    /* 回复图文消息
     * $msgArray格式
     * $msgArray = array(
     * 		array('项目标题', '描述', '图片地址', '点击项目打开的Url'),
     * 		array('项目标题', '描述', '图片地址', '点击项目打开的Url'),
     *      有几个项目就设置几个数组元素
     * );
     */
    public function reItemMsgs($msgArray){
	$xml = '<xml><ToUserName><![CDATA['.$this->openId.']]></ToUserName><FromUserName><![CDATA['.$this->ourOpenId.']]></FromUserName><CreateTime>'.time().'</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>'.count($msgArray).'</ArticleCount><Articles>';
        foreach($msgArray as $val){
	    $xml .= '<item><Title><![CDATA['.$val[0].']]></Title><Description><![CDATA['.$val[1].']]></Description><PicUrl><![CDATA['.$val[2].']]></PicUrl><Url><![CDATA['.$val[3].']]></Url></item>';
	}
	$xml .= '</Articles></xml>';
	echo $xml;
    }
    //回复文本消息
    public function reTextMsg($msg){
	$xml = '<xml><ToUserName><![CDATA['.$this->openId.']]></ToUserName><FromUserName><![CDATA['.$this->ourOpenId.']]></FromUserName><CreateTime>'.time().'</CreateTime>
<Msgype><![CDATA[text]]></MsgType><Content><![CDATA['.$msg.']]></Content></xml>';
    	echo $xml;
    }
    //接口认证函数
    public function valid(){
	$echoStr = $_GET["echostr"];
	if($this->checkSignature()){echo $echoStr; exit();}
    }
    //认证消息签名认证函数
    private function checkSignature(){
	if (!defined("TOKEN")){throw new Exception('TOKEN is not defined!');}
	$signature = $_GET["signature"];
	$timestamp = $_GET["timestamp"];
	$nonce = $_GET["nonce"];
	$token = TOKEN;
	$tmpArr = array($token, $timestamp, $nonce);
	sort($tmpArr, SORT_STRING);
	$tmpStr = implode($tmpArr);
	$tmpStr = sha1( $tmpStr );
	if($tmpStr == $signature ){return true;}else{return false;}
    }
    //curl get 方式 封装
    public function curlGet($url){
	$ch   = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
	curl_setopt($ch, CURLOPT_ENCODING       , 'gzip,deflate');
	$res  = curl_exec($ch);
	$this->curlStatus = curl_getinfo($ch);
	curl_close($ch);
	return $res;
    }
    /*
     * curl POST 方式
     * 参数1 $url
     * 参数2 $data 格式 array('name'=>'test', 'age' => 18)
     */
    public function curlPost($url, $data){
    	$ch   = curl_init();
    	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER , true);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST , false);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_ENCODING       , 'gzip,deflate');
	$res  = curl_exec($ch);
	$this->curlStatus = curl_getinfo($ch);
	curl_close($ch);
	return $res;
    }
}


书籍目录  (请点击阅读)