php工具类备份
1,接收post请求json数据
$postData = file_get_contents('php://input');
$data = json_decode($postData, true);
$good_id = $data['goods_id'];
2,发送post请求
/**
* 发送post请求
* @param $data 发送参数array
* @param $url 发送url
* @param int $type
* @return $rtn 返回信息
*/
public function postJsonCurl($data,$url,$type=1)
{
$headers = array("Content-type: application/json","Accept: application/json","Cache-Control: no-cache","Pragma: no-cache");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 60); //设置超时
if(0 === strpos(strtolower($url), 'https')) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //从证书中检查SSL加密算法是否存在
}
if($type){
curl_setopt($ch, CURLOPT_POST, TRUE);
$res_json = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", json_encode($data));
$res_json = str_replace("\\/", "/", $res_json);
curl_setopt($ch, CURLOPT_POSTFIELDS, $res_json);
}else{
curl_setopt($ch, CURLOPT_POST, false);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$rtn = curl_exec($ch);//CURLOPT_RETURNTRANSFER 不设置 curl_exec返回TRUE 设置 curl_exec返回json(此处) 失败都返回FALSE
curl_close($ch);
return $rtn;
}
3,生成32位随机数
function createNoncestr( $length = 32 ){
$chars ="abcdefghijklmnopqrstuvwxyz0123456789";
$str ="";
for($i=0;$i<$length;$i++){
$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
4,校验字段是否存在
if (!isset($res['phone'])) {
$data = array('return_code' => '2000', 'return_msg' => '参数不正确,手机号不存在');
return $data;
}
5,删除数组中的元素
//删除goods数组中的good_name元素
unset($goods['good_name']);