I Am I
I Am I
文章88
标签95
分类4
PHP使用CURL模拟GET和POST请求

PHP使用CURL模拟GET和POST请求

最近在写短视频去水印(https://lab.5ime.cn/video) 记录一下常用的Curl

GET请求

function Get_curl($url)
{
    $Header=array( "User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    $ch=curl_init((string)$url);
    curl_setopt($ch,CURLOPT_HEADER,False);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,False);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$Header);
    curl_setopt($ch,CURLOPT_TIMEOUT,5000);
    $result = curl_exec($ch);
    return $result;
}
function Get_curl($url)
{
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate, br";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "Referer: ". $url;
$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, $url); //设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"); //设置请求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;
}

POST请求

function Post_curl($url , $post_data = array("msg" => "123","data" => "456",)){
    $Header=array( "User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch,CURLOPT_HTTPHEADER,$Header);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}
function Post_curl($url)
{
$ip = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$header[] = "accept: application/json";
$header[] = "accept-encoding: gzip, deflate, br";
$header[] = "accept-language: en-US,en;q=0.8";
$header[] = "content-type: application/json";
$header[] = "Referer: ". $url;
$header[] = "CLIENT-IP:" . $ip;
$header[] = "X-FORWARDED-FOR:" . $ip;
$cookie = "cookie";
$post_data = array(
    "msg" => "123",
    "data" => "456"
);
//$post_data = "{\"pid\":".$video_id.",\"type\":\"post\",\"mid\":null}";
$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, $url);//设置Referer
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");//设置请求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;
}