直接基于curl發(fā)起請(qǐng)求就可以了,至于每次推送一次的需求,你可以通過(guò)定時(shí)去做,每次
目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、慈溪網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
只執(zhí)行一遍即可。
關(guān)于php?curl更多內(nèi)容,請(qǐng)參考我的博客《PHP?cURL實(shí)現(xiàn)模擬登錄與采集使用方法詳解教程》
?php
use Workerman\Worker;
require_once './Workerman/Autoloader.php';
// 初始化一個(gè)worker容器,監(jiān)聽(tīng)1234端口
$worker = new Worker('websocket://0.0.0.0:1234');
// 這里進(jìn)程數(shù)必須設(shè)置為1
$worker-count = 1;
// worker進(jìn)程啟動(dòng)后建立一個(gè)內(nèi)部通訊端口
$worker-onWorkerStart = function($worker)
{
// 開(kāi)啟一個(gè)內(nèi)部端口,方便內(nèi)部系統(tǒng)推送數(shù)據(jù),Text協(xié)議格式 文本+換行符
$inner_text_worker = new Worker('Text://0.0.0.0:5678');
$inner_text_worker-onMessage = function($connection, $buffer)
{
global $worker;
// $data數(shù)組格式,里面有uid,表示向那個(gè)uid的頁(yè)面推送數(shù)據(jù)
$data = json_decode($buffer, true);
$uid = $data['uid'];
// 通過(guò)workerman,向uid的頁(yè)面推送數(shù)據(jù)
$ret = sendMessageByUid($uid, $buffer);
// 返回推送結(jié)果
$connection-send($ret ? 'ok' : 'fail');
};
$inner_text_worker-listen();
};
// 新增加一個(gè)屬性,用來(lái)保存uid到connection的映射
$worker-uidConnections = array();
// 當(dāng)有客戶端發(fā)來(lái)消息時(shí)執(zhí)行的回調(diào)函數(shù)
$worker-onMessage = function($connection, $data)use($worker)
{
// 判斷當(dāng)前客戶端是否已經(jīng)驗(yàn)證,既是否設(shè)置了uid
if(!isset($connection-uid))
{
// 沒(méi)驗(yàn)證的話把第一個(gè)包當(dāng)做uid(這里為了方便演示,沒(méi)做真正的驗(yàn)證)
$connection-uid = $data;
/* 保存uid到connection的映射,這樣可以方便的通過(guò)uid查找connection,
* 實(shí)現(xiàn)針對(duì)特定uid推送數(shù)據(jù)
*/
$worker-uidConnections[$connection-uid] = $connection;
return;
}
};
// 當(dāng)有客戶端連接斷開(kāi)時(shí)
$worker-onClose = function($connection)use($worker)
{
global $worker;
if(isset($connection-uid))
{
// 連接斷開(kāi)時(shí)刪除映射
unset($worker-uidConnections[$connection-uid]);
}
};
// 向所有驗(yàn)證的用戶推送數(shù)據(jù)
function broadcast($message)
{
global $worker;
foreach($worker-uidConnections as $connection)
{
$connection-send($message);
}
}
// 針對(duì)uid推送數(shù)據(jù)
function sendMessageByUid($uid, $message)
{
global $worker;
if(isset($worker-uidConnections[$uid]))
{
$connection = $worker-uidConnections[$uid];
$connection-send($message);
return true;
}
return false;
}
// 運(yùn)行所有的worker(其實(shí)當(dāng)前只定義了一個(gè))
Worker::runAll();
啟動(dòng)后端服務(wù)
php push.php start -d
前端接收推送的js代碼
var ws = new WebSocket('ws://127.0.0.1:1234');
ws.onopen = function(){
var uid = 'uid1';
ws.send(uid);
};
ws.onmessage = function(e){
alert(e.data);
};
后端推送消息的代碼
// 建立socket連接到內(nèi)部推送端口
$client = stream_socket_client('tcp://127.0.0.1:5678', $errno, $errmsg, 1, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);
// 推送的數(shù)據(jù),包含uid字段,表示是給這個(gè)uid推送
$data = array('uid'='uid1', 'percent'='88%');
// 發(fā)送數(shù)據(jù),注意5678端口是Text協(xié)議的端口,Text協(xié)議需要在數(shù)據(jù)末尾加上換行符
fwrite($client, json_encode($data)."\n");
// 讀取推送結(jié)果
echo fread($client, 8192);
這里的uid不一定是用戶的id,也可以理解為任務(wù)id即 taskid
以上代碼親測(cè)可以直接使用
在調(diào)用公眾號(hào)接口".$token."type=".$type;
上傳永久素材文件總是返回 "{\"errcode\":41005,\"errmsg\":\"media data missing\"}"
經(jīng)過(guò)多次測(cè)試使用下面的方式,可以正常上傳
//調(diào)用測(cè)試
protected static $url;
protected static $delimiter;
protected static $instance;
public function index()
{
? ? static::$delimiter = uniqid();
$basename = Request::instance()-root();
if (pathinfo($basename, PATHINFO_EXTENSION) == 'php') {
$basename = dirname($basename);
}
? ? $result=$this-wxAddMaterial($token,$basename.'/upload/images/gnlog.jpg','image');
}
// 新增其他類型永久素材
public function wxAddMaterial($token,$filename='',$type='') {
// 設(shè)置請(qǐng)求參數(shù)
static::$url = "".$token."type=".$type;
$filePath = str_replace('\\', '/', $filename);
// 發(fā)送請(qǐng)求
$imginfo=pathinfo($filePath);
$fields = array(
? ? 'media'=file_get_contents(".".$filePath),
? ? 'filename'=$imginfo["basename"],
);
$res = $this-putPart( $fields);
// 發(fā)送請(qǐng)求
return $res;
}
//推送文件流
public function putPart($param) {
$post_data = static::buildData($param);
$curl = curl_init(static::$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
? ? "Content-Type: multipart/form-data; boundary=" . static::$delimiter,
? ? "Content-Length: " . strlen($post_data)
]);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
//編譯請(qǐng)求頭格式和數(shù)據(jù)流
private static function buildData($param){
$data = '';
$eol = "\r\n";
$upload = $param['media'];
unset($param['media']);
foreach ($param as $name = $content) {
? ? $data .= "--" . static::$delimiter . "\r\n"
? ? ? ? . 'Content-Disposition: form-data; name="' . $name . "\"\r\n\r\n"
? ? ? ? . $content . "\r\n";
}
$data .= "--" . static::$delimiter . $eol
? ? . 'Content-Disposition: form-data; name="media"; filename="' . $param['filename'] . '"' . "\r\n"
? ? . 'Content-Type:application/octet-stream'."\r\n\r\n";
$data .= $upload . "\r\n";
$data .= "--" . static::$delimiter . "--\r\n";
return $data;
}
根據(jù)自己的實(shí)際情況稍作修改