PHP CURL模拟GET及POST函数代码
程序员文章站
2022-10-06 09:01:23
复制代码 代码如下:
<?php
function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
$tmpinfo = '';
$cookiepath = getcwd().'./'.$cookiejar;
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']);
if($referer) {
curl_setopt($curl, curlopt_referer, $referer);
} else {
curl_setopt($curl, curlopt_autoreferer, 1);
}
if($post) {
curl_setopt($curl, curlopt_post, 1);
curl_setopt($curl, curlopt_postfields, $post);
}
if($cookie) {
curl_setopt($curl, curlopt_cookie, $cookie);
}
if($cookiejar) {
curl_setopt($curl, curlopt_cookiejar, $cookiepath);
curl_setopt($curl, curlopt_cookiefile, $cookiepath);
}
//curl_setopt($curl, curlopt_followlocation, 1);
curl_setopt($curl, curlopt_timeout, 100);
curl_setopt($curl, curlopt_header, 0);
curl_setopt($curl, curlopt_returntransfer, 1);
$tmpinfo = curl_exec($curl);
if (curl_errno($curl)) {
echo '<pre><b>错误:</b><br />'.curl_error($curl);
}
curl_close($curl);
return $tmpinfo;
}
?>
复制代码 代码如下:
<?php
function vcurl($url, $post = '', $cookie = '', $cookiejar = '', $referer = ''){
$tmpinfo = '';
$cookiepath = getcwd().'./'.$cookiejar;
$curl = curl_init();
curl_setopt($curl, curlopt_url, $url);
curl_setopt($curl, curlopt_useragent, $_server['http_user_agent']);
if($referer) {
curl_setopt($curl, curlopt_referer, $referer);
} else {
curl_setopt($curl, curlopt_autoreferer, 1);
}
if($post) {
curl_setopt($curl, curlopt_post, 1);
curl_setopt($curl, curlopt_postfields, $post);
}
if($cookie) {
curl_setopt($curl, curlopt_cookie, $cookie);
}
if($cookiejar) {
curl_setopt($curl, curlopt_cookiejar, $cookiepath);
curl_setopt($curl, curlopt_cookiefile, $cookiepath);
}
//curl_setopt($curl, curlopt_followlocation, 1);
curl_setopt($curl, curlopt_timeout, 100);
curl_setopt($curl, curlopt_header, 0);
curl_setopt($curl, curlopt_returntransfer, 1);
$tmpinfo = curl_exec($curl);
if (curl_errno($curl)) {
echo '<pre><b>错误:</b><br />'.curl_error($curl);
}
curl_close($curl);
return $tmpinfo;
}
?>