<?php
namespace Org\Util;
class Netease{
private $AppKey; //开发者平台分配的AppKey
private $AppSecret; //开发者平台分配的AppSecret,可刷新
private $Nonce; //随机数(最大长度128个字符)
private $CurTime; //当前UTC时间戳,从1970年1月1日0点0 分0 秒开始到现在的秒数(String)
private $CheckSum; //SHA1(AppSecret + Nonce + CurTime),三个参数拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)
const HEX_DIGITS = "0123456789abcdef";
/**
* @method 参数初始化
* @param $AppKey
* @param $AppSecret
* @param $RequestType [选择php请求方式,fsockopen或curl,若为curl方式,请检查php配置是否开启]
*/
public function __construct($AppKey,$AppSecret,$RequestType='curl'){
$this->AppKey = $AppKey;
$this->AppSecret = $AppSecret;
$this->RequestType = $RequestType;
}
/**
* @method API checksum校验生成
* @param void
* @return $CheckSum(对象私有属性)
*/
public function checkSumBuilder(){
//此部分生成随机字符串
$hex_digits = self::HEX_DIGITS;
$this->Nonce;
for($i=0;$i<128;$i++){ //随机字符串最大128个字符,也可以小于该数
$this->Nonce.= $hex_digits[rand(0,15)];
}
$this->CurTime = (string)(time()); //当前时间戳,以秒为单位
$join_string = $this->AppSecret.$this->Nonce.$this->CurTime;
$this->CheckSum = sha1($join_string);
//print_r($this->CheckSum);
}
/**
* @method 将json字符串转化成php数组
* @param $json_str
* @return $json_arr
*/
public function json_to_array($json_str){
// version 1.6 code ...
// if(is_null(json_decode($json_str))){
// $json_str = $json_str;
// }else{
// $json_str = json_decode($json_str);
// }
// $json_arr=array();
// //print_r($json_str);
// foreach($json_str as $k=>$w){
// if(is_object($w)){
// $json_arr[$k]= $this->json_to_array($w); //判断类型是不是object
// }else if(is_array($w)){
// $json_arr[$k]= $this->json_to_array($w);
// }else{
// $json_arr[$k]= $w;
// }
// }
// return $json_arr;
if(is_array($json_str) || is_object($json_str)){
$json_str = $json_str;
}else if(is_null(json_decode($json_str))){
$json_str = $json_str;
}else{
$json_str = strval($json_str);
$json_str = json_decode($json_str,true);
}
$json_arr=array();
foreach($json_str as $k=>$w){
if(is_object($w)){
$json_arr[$k]= $this->json_to_array($w); //判断类型是不是object
}else if(is_array($w)){
$json_arr[$k]= $this->json_to_array($w);
}else{
$json_arr[$k]= $w;
}
}
return $json_arr;
}
/**
* 使用CURL方式发送post请求
* @param $url [请求地址]
* @param $data [array格式数据]
* @return $请求返回结果(array)
*/
public function postDataCurl($url,$data){
$this->checkSumBuilder(); //发送请求前需先生成checkSum
$timeout = 5000;
$http_header = array(
'AppKey:'.$this->AppKey,
'Nonce:'.$this->Nonce,
'CurTime:'.$this->CurTime,
'CheckSum:'.$this->CheckSum,
'Content-Type:application/x-www-form-urlencoded;charset=utf-8'
);
//print_r($http_header);
// $postdata = '';
$postdataArray = array();
foreach ($data as $key=>$value){
array_push($postdataArray, $key.'='.urlencode($value));
// $postdata.= ($key.'='.urlencode($value).'&');
}
$postdata = join('&', $postdataArray);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_HEADER, false );
curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (false === $result) {
$result = curl_errno($ch);
}
curl_close($ch);
return $this->json_to_array($result) ;
}
/**
* 使用FSOCKOPEN方式发送post请求
* @param $url [请求地址]
* @param $data [array格式数据]
* @return $请求返回结果(array)
*/
public function postDataFsockopen($url,$data){
$this->checkSumBuilder(); //发送请求前需先生成checkSum
// $postdata = '';
$postdataArray = array();
foreach ($data as $key=>$value){
array_push($postdataArray, $key.'='.urlencode($value));
// $postdata.= ($key.'='.urlencode($value).'&');
}
$postdata = join('&', $postdataArray);
// building POST-request:
$URL_Info=parse_url($url);
if(!isset($URL_Info["port"])){
$URL_Info["port"]=80;
}
$request = '';
$request.="POST ".$URL_Info["path"]." HTTP/1.1\r\n";
$request.="Host:".$URL_Info["host"]."\r\n";
$request.="Content-type: application/x-www-form-urlencoded;charset=utf-8\r\n";
$request.="Content-length: ".strlen($postdata)."\r\n";
$request.="Connection: close\r\n";
$request.="AppKey: ".$this->AppKey."\r\n";
$request.="Nonce: ".$this->Nonce."\r\n";
$request.="CurTime: ".$this->CurTime."\r\n";
$request.="CheckSum: ".$this->CheckSum."\r\n";
$request.="\r\n";
$request.=$postdata."\r\n";
// print_r($request);
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
$result = '';
while(!feof($fp)) {
$result .= fgets($fp, 128);
}
fclose($fp);
$str_s = strpos($result,'{');
$str_e = strrpos($result,'}');
$str = substr($result, $str_s,$str_e-$str_s+1);
return $this->json_to_array($str);
}
/**
* 使用CURL方式发送post请求(JSON类型)
* @param $url [请求地址]
* @param $data [array格式数据]
* @return $请求返回结果(array)
*/
public function postJsonDataCurl($url,$data){
$this->checkSumBuilder(); //发送请求前需先生成checkSum
$timeout = 5000;
$http_header = array(
'AppKey:'.$this->AppKey,
'Nonce:'.$this->Nonce,
'CurTime:'.$this->CurTime,
'CheckSum:'.$this->CheckSum,
'Content-Type:application/json;charset=utf-8'
);
//print_r($http_header);
$postdata = json_encode($data);
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt ($ch, CURLOPT_HEADER, false );
curl_setopt ($ch, CURLOPT_HTTPHEADER,$http_header);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,false); //处理http证书问题
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (false === $result) {
$result = cur
网易云信接口开发 (php SDK) 带示例文件
5星 · 超过95%的资源 需积分: 50 127 浏览量
2017-12-08
15:27:56
上传
评论
收藏 10KB ZIP 举报
yvsm316
- 粉丝: 3
- 资源: 8
最新资源
- 基于python实现的基于PyQt5和爬虫的小说阅读系统.zip
- 机械设计整经机上纱自动化sw20非常好的设计图纸100%好用.zip
- Screenshot_20240427_031602.jpg
- 网页PDF_2024年04月26日 23-46-14_QQ浏览器网页保存_QQ浏览器转格式(6).docx
- 直接插入排序,冒泡排序,直接选择排序.zip
- 在排序2的基础上,再次对快排进行优化,其次增加快排非递归,归并排序,归并排序非递归版.zip
- 实现了7种排序算法.三种复杂度排序.三种nlogn复杂度排序(堆排序,归并排序,快速排序)一种线性复杂度的排序.zip
- 冒泡排序 直接选择排序 直接插入排序 随机快速排序 归并排序 堆排序.zip
- 课设-内部排序算法比较 包括冒泡排序、直接插入排序、简单选择排序、快速排序、希尔排序、归并排序和堆排序.zip
- Python排序算法.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈