进行curl请求需要主要的问题

来源:互联网 发布:金山恢复数据恢复软件 编辑:程序博客网 时间:2024/05/19 06:15

1、在进行curl请求的时候,如果对方对数据格式有要求

    //1.初始化url
    $ch = curl_init($url);
    //2.设置相关的参数
    //字符串不直接输出,进行一个变量的存储
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //判断是否为https请求
    if($https === true){
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    //判断是否为post请求
    if($method == 'post'){
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
               'Content-Type: application/json;charset=UTF-8',在这边要设置请求的数据格式,否则容易是xml类型的
               'Content-Length:' . strlen($data))
      );
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //3.发送请求
    $str = curl_exec($ch);
    //4.关闭连接
    curl_close($ch);
    //返回请求到的结果
    return $str;

阅读全文
0 0
原创粉丝点击