curl和Http Header里的Content-Type

使用函数之前我们要需要把php curl模块打开(libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll)

为cURL传输会话批量设置选项。这个函数对于需要设置大量的cURL选项是非常有用的,不需要重复地调用curl_setopt()。

foreach循环中用

array(
 CURLOPT_URL => "http://www.xx.com/addTicket.jsp", //访问URL
 CURLOPT_RETURNTRANSFER => true, //获取结果作为字符串返回
 CURLOPT_REFERER => "ww.ww.ww/zw2",
 CURLOPT_HTTPHEADER => array('X-FORWARDED-FOR:139.197.14.19', 'CLIENT-IP:127.0.0.1','Proxy-Client-IP:139.197.14.19','WL-Proxy-Client-IP:139.197.14.19' ),
 CURLOPT_HEADER => 1, //获取返回头信息
 //CURLOPT_SSL_VERIFYPEER => false, //支持SSL加密
 CURLOPT_POST => true, //发送时带有POST参数
 CURLOPT_POSTFIELDS => 'ids=897&Submit=%E6%8A%95%E7%A5%A8', //请求的POST参数字符串
 CURLOPT_TIMEOUT => $aa->timeout //等待响应的时间
 );

 

$options = array(
CURLOPT_POST => 1,
// CURLOPT_POSTFIELDS => http_build_query($paramsArr), // 注入接口参数
CURLOPT_POSTFIELDS => $postdata, // 注入接口参数
CURLOPT_URL => $url, // 请求URL
CURLOPT_RETURNTRANSFER => 1, // 获取请求结果
CURLOPT_TIMEOUT_MS => 6000000, // 超时时间(ms)
// -----------请确保启用以下两行配置------------
CURLOPT_SSL_VERIFYPEER => FALSE, // 验证证书
CURLOPT_SSL_VERIFYHOST => 2, // 验证主机名
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);

Http Header里的Content-Type
转:https://www.cnblogs.com/52fhy/p/5436673.html

/**
 * curl 函数
 * @param string $url 请求的地址
 * @param string $type POST/GET/post/get
 * @param array $data 要传输的数据
 * @param string $err_msg 可选的错误信息(引用传递)
 * @param int $timeout 超时时间
 * @param array 证书信息
 * @author 勾国印
 */
function GoCurl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array())
{
    $type = strtoupper($type);
    if ($type == 'GET' && is_array($data)) {
        $data = http_build_query($data);
    }

    $option = array();

    if ( $type == 'POST' ) {
        $option[CURLOPT_POST] = 1;
    }
    if ($data) {
        if ($type == 'POST') {
            $option[CURLOPT_POSTFIELDS] = $data;
        } elseif ($type == 'GET') {
            $url = strpos($url, '?') !== false ? $url.'&'.$data :  $url.'?'.$data;
        }
    }

    $option[CURLOPT_URL]            = $url;
    $option[CURLOPT_FOLLOWLOCATION] = TRUE;
    $option[CURLOPT_MAXREDIRS]      = 4;
    $option[CURLOPT_RETURNTRANSFER] = TRUE;
    $option[CURLOPT_TIMEOUT]        = $timeout;

    //设置证书信息
    if(!empty($cert_info) && !empty($cert_info['cert_file'])) {
        $option[CURLOPT_SSLCERT]       = $cert_info['cert_file'];
        $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass'];
        $option[CURLOPT_SSLCERTTYPE]   = $cert_info['cert_type'];
    }

    //设置CA
    if(!empty($cert_info['ca_file'])) {
        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
        $option[CURLOPT_SSL_VERIFYPEER] = 1;
        $option[CURLOPT_CAINFO] = $cert_info['ca_file'];
    } else {
        // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO
        $option[CURLOPT_SSL_VERIFYPEER] = 0;
    }

    $ch = curl_init();
    curl_setopt_array($ch, $option);
    $response = curl_exec($ch);
    $curl_no  = curl_errno($ch);
    $curl_err = curl_error($ch);
    curl_close($ch);

    // error_log
    if($curl_no > 0) {
        if($err_msg !== null) {
            $err_msg = '('.$curl_no.')'.$curl_err;
        }
    }
    return $response;
}
$url   = '请求地址';
$data = array(
            'phoneNum' => '18614064456',
        );
$json = GoCurl($url, $data, 'POST', $error_msg);

$array = json_decode($json, true);

print_r($array);

 

posted @ 2019-04-12 11:42  星云惊蛰  阅读(4837)  评论(0编辑  收藏  举报