PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据

PHP简单的Curl的Get请求和Curl的Post请求和file_get_contents的Get请求获取接口JSON数据

韩小韩
2019-07-19 / 2 评论 / 2,139 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年01月06日,已超过256天没有更新,若内容或图片失效,请留言反馈。

PHP携带Cookie用Curl进行Post或Get请求获取数据

PHP全能Curl请求

/**
 * curl发送HTTP请求方法
 * @param $url
 * @param string $method
 * @param array $params
 * @param array $header
 * @param int $timeout
 * @param bool|false $multi
 * @return mixed
 * @throws Exception
 */
function hansCurl( $url, $method = 'GET', $params = array(), $header = array(),
                                 $timeout = 30, $multi = false ) {
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $header );
    switch ( strtoupper( $method ) ) {
        case 'GET':
            if ( !empty( $params ) ) {
                $uri = parse_url( $url );
                $url .= ( empty( $uri[ 'query' ] ) ? '?' : '&' ) . http_build_query( $params );
            }
            curl_setopt( $curl, CURLOPT_URL, $url );
            break;
        case 'POST':
            curl_setopt( $curl, CURLOPT_URL, $url );
            curl_setopt( $curl, CURLOPT_POST, true );
            $params = $multi ? $params : http_build_query( $params );  //判断是否传输文件
            curl_setopt( $curl, CURLOPT_POSTFIELDS, $params );
            break;
        default:
            throw new Exception( '不支持的请求方式!' );
    }
    $response = curl_exec( $curl );
    $error = curl_error( $curl );
    curl_close( $curl );
    if ( $error ) {
        throw new Exception( '请求发生错误:' . $error );
    }
    return $response;
}

简单的curl请求(Get请求)


<?php
function hansCurl($url)
{
$url="https://www.vvhan.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //设置传输的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //发送 http 报头
curl_setopt($ch, CURLOPT_COOKIE, $cookie); //设置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com"); //设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36"); //设置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

简单的curl请求(Post请求)


<?php
function hansCurl($url)
{
$url="https://www.vvhan.com";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie="cookie";
$post_data = array(
    "token" => "123456"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);//设置传输的 url
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //发送 http 报头
curl_setopt($ch, CURLOPT_COOKIE, $cookie);//设置Cookie
curl_setopt($ch, CURLOPT_REFERER, "https://www.vvhan.com");//设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36");//设置UA
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); // 解码压缩文件
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);// 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_POST, 1); //设置POST发送数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);//发送POST数据内容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

PHP的curl上传文件(Post请求)

<?php
function hansCurl($url)
{
$url = "https://www.vvhan.com";
$names="1.png";
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$post_data = [
    'name' => $names,
    'attrFile' => new CURLFile(realpath('hanCURLFile/' . $names)),
];
$ch = curl_init(); // 启动一个CURL会话
curl_setopt($ch, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'); // 模拟用户使用的浏览器
curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($ch, CURLOPT_REFERER, 'https://www.vvhan.com'); // 自动设置Referer
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); // Post提交的数据包
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环
curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
//curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie/cookie.txt");//保存cookie文件
//curl_setopt($ch, CURLOPT_COOKIEFILE, "./cookie/cookie.txt"); //调用cookie文件
$output = curl_exec($ch); // 执行操作
curl_close($ch); // 关闭CURL会话
return ($output);
}
?>

简单的file_get_contents请求(Get请求)


<?php
$url = 'https://www.vvhan.com/';
$data = file_get_contents($url);
exit($data);
?>
1

评论 (2)

取消
  1. 头像
    1287919171
    Android Pie · QQ Browser

    晚上好。哈哈

    回复
    1. 头像
      小韩啊 作者
      iPhone · QQ Browser
      @ 1287919171

      哈哈。下午好

      回复