阅读cURL here 的文档。它具有可用于 POST 数据、cookie 和 HTTPS 的选项。
但是,为了帮助您,这是我用来发出 cURL 请求的函数。您需要根据自己的目的修改选项,例如您想要启用 HTTPS 选项,并且您可能不想存储 cookie。不过请阅读文档!
function curl_request($url, $referer = false, $postdata = false, $new_session = false) //single custom cURL request.
{
$ch = curl_init();
if ($new_session)
{
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
}
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookies.txt");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
if ($referer)
{
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
if ($postdata)
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
另外,如果需要将 cookie 变量放入 php 变量中,请遵循 answer here。仔细考虑检查 cookie 是否是检查登录的最佳方式。