php curl 模拟get请求 并设置header

1. 模拟get请求文件 test_get.php

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
function http_get($url)
{
    $headers[] = "Content-type: application/x-www-form-urlencoded";
    $headers[] = "Zoomkey-Auth-Token: 9CD0F0F60AFDF00";
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $tmpInfo = curl_exec($curl);     
    //关闭URL请求
    curl_close($curl);
    return $tmpInfo;   
}
$url = ‘http://www.test.com/test_get_info.php?name=123‘;
$resu = http_get($url);
echo $resu;

2. 测试文件 test_get_info.php

<?php
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
$name = $_GET[‘name‘];
if($name)
{
    // echo "{\"name\":$name,\"s\":0}";
    // getallheaders()函数 php4 php5支持   apache支持 iis和nginx不支持
    foreach (getallheaders() as $name => $value) {
        echo "$name: $value\n";
    }
}else
{
    echo "{\"s\":-3}";
}

相关推荐