fsockopen(使用)

本文介绍了一种基于SHA1的签名验证方法,用于确保HTTP POST请求的安全性。通过使用特定的参数组合(包括token、timestamp、client_id和nonceStr),生成一个签名(signaure),并将其作为请求的一部分发送,以验证请求的完整性和来源。文章还提供了使用PHP实现的示例代码。

https http
443 80

//signaure 加密方式
$signaure = sha1($token.$timestamp.$client_id.$nonceStr);
$data['token'] = $token;
$data['nonce'] = $nonceStr;
$data['timestamp'] = $timestamp;
$data['client_id'] = $client_id;
$data['signaure'] = $signaure;
//$data = json_encode($data,JSON_UNESCAPED_UNICODE);


$params = isset($data) ? http_build_query($data) : '';

$fp = fsockopen('ssl://admin.yifengaf.cn', 443, $error, $errstr, 1);
stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
stream_set_timeout($fp,1);//设置超时
$http = "POST /api/channeldataapi/custom HTTP/1.1\r\nHost: admin.yifengaf.cn\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
fwrite($fp, $http);
//usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
while(!feof($fp))
{
    echo fgets($fp, 1024);
}
fclose($fp);
   for ($i = 0; $i < $num; $i++) {
                    $data  = array(
                      'touser' => $fansInfo[$i]['openid'],
//                     "touser"=>'oncqX1ft1xDsAakmqyShSUwiJGOI',
                        'template_id' => $optionInfo['tpl_id'],
                        'url' => $optionInfo['url'],
                        'data' => $postdata
                    );
                    $params = json_encode($data,JSON_UNESCAPED_UNICODE);
                    $fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
                    stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
       				 stream_set_timeout($fp,1);//设置超时
                    $http = "POST /cgi-bin/message/template/send?access_token={$accessToken} HTTP/1.1\r\nHost: api.weixin.qq.com\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
                    fwrite($fp, $http);
                  	 usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
                    fclose($fp);

//                        $result = $accObj->sendTplNotice('o_aCh1R9-lur2VEQXJOSXZXK6-c8', $optionInfo['tpl_id'], $postdata, $optionInfo['url'], $optionInfo['topcolor']);
//                    $result = $this->sendTplMsg('o_aCh1ZR6b8Qh0U_GmjAHtyQ-wrc', $optionId);
                    if ($fp) {
                        $k = $i + 1;
                    }
                }

$data['channel_id'] = '11111';
$data['search_date'] = '20200315';  //时间格式 date("Ymd") 如 20200309

$res = $this->sendfsockopen('custom','POST',$data);
print_r($res);die;




 public function sendfsockopen( $path = '',$method = 'GET', $data = [],  $timeout = 30){
        $client_id = '111111';
        $token =   '111111111';
        $nonceStr = random(25);
        $timestamp = TIMESTAMP;
        //signaure 加密方式
        $signaure = sha1($token.$timestamp.$client_id.$nonceStr);
        $data['token'] = $token;
        $data['nonce'] = $nonceStr;
        $data['timestamp'] = $timestamp;
        $data['client_id'] = $client_id;
        $data['signaure'] = $signaure;
        $params = isset($data) ? http_build_query($data) : '';

        $url = 'admin.yifengaf.cn';
        $path  = '/api/channeldataapi/' .$path;

        $fp = fsockopen('ssl://' .$url, 443, $error, $errstr, $timeout);
        stream_set_blocking($fp,true);//开启了手册上说的非阻塞模式
        stream_set_timeout($fp,1);//设置超时
        $info = stream_get_meta_data($fp);
        $http = "{$method} {$path} HTTP/1.1\r\nHost: {$url}\r\nContent-type: application/x-www-form-urlencoded\r\nContent-Length: " . strlen($params) . "\r\nConnection:close\r\n\r\n$params\r\n\r\n";
        fwrite($fp, $http);
//usleep(1000); // 这一句也是关键,如果没有这延时,可能在nginx服务器上就无法执行成功
        while ((!feof($fp))) {
            $data .= fgets($fp, 8192);
            @ob_flush();
            flush();
        }
        $contentInfo = explode("\n\n", str_replace("\r", "", $data));
        if (!strstr($contentInfo[0], "HTTP/1.1 200 OK")) {
            return -10;
        }
        return json_decode(trim($contentInfo[1]),true);
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值