简化 PHP Curl

CURL
用法:

c_get(URL,方式(post/get),要传输的数据,referer,timeout(秒),是否使用cookie,是否保存cookie,cookie保存到什么路径)

例如:

//POST METHOD :D
c_get(
    "https://localhost/testlogin.php",
    'post',
    array("name" => "username","pass" => "userpassword"),
    "",
    15,
    false,//不传输cookie
    true,//保存cookie
    "testlogin.ck.txt"
);
//GET METHOD :D
$ct = c_get(
    "https://localhost/testget.php?v1=val2333&v2=val1248",
    'get',
    "使用get的话,这里的data不用填,而是填到url那里去",
    "https://www.referer.mother",
    20,
    true,//传输cookie
    false,//不保存cookie
    "ck/6zhen.txt"
);

复制以下代码:

function c_get($url,$method,$data,$referer='https://www.baidu.com/',$timeout=15,$useck=false,$saveck=false,$ckfile='ck.txt'){
/*$url = "";
$data = array (
  "swapname" => "name",
  "swappass" => "password"
);
$referer = "https://www.baidu.com/";
*/
$header = array("User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $timeout*1000);//超时毫秒 ms
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER,$referer);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER,true);//是否输出头部信息
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

if ($saveck == true) {
    curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
}
if (file_exists($ckfile) && $useck == true) {
    curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
}
if ($method == "post") {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$content = curl_exec($ch);
if(curl_errno($ch)){
    return 'Curl error: '.curl_error($ch);
}
if ($content == false) {
    return "Get content false!";
}
return $content;
curl_close($ch);
}

标签: 推荐

添加新评论