php参数获取及post请求发送

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']);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值