感触life

封装个php curl post方法

参数说明

$url链接  $data数组数据

    public function curl_post($url,$data)
    {   
        if(!is_array($data)){
          //throw new Exception("参数必须为array");
          echo "参数必须为array";
        }
        $data =  json_encode($data);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data)
        ));

          $result = curl_exec($ch);
          if (curl_errno($ch)) {
              print curl_error($ch);
          }
          curl_close($ch);
          return $result;
    }

码字很辛苦,转载请注明来自感触life-博客《封装个php curl post方法》

评论

  1. uu #1

    :hgu:

    回复
    2021-06-16