ببینید، برای مثال این تابع sURL که من برای کارهای خودم نوشتم، خیلی در این زمینه میشه گفت کامله:
کد HTML:
<?php
function sURL($url, $post = false, $cookie = false, $user = false, $pass = false, $userAgent = false, $headerOnly = false, $timeOut = 30) {
$pUrl = parse_url($url);
$userAgent = ($userAgent == false) ? 'Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.02' : $userAgent;
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, true);
if ($headerOnly != false) {
curl_setopt($curl, CURLOPT_NOBODY, true);
}
if ($pUrl['scheme'] == 'https') {
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
}
if ($post != false && is_array($post)) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
}
if ($cookie != false && is_array($cookie)) {
curl_setopt($curl, CURLOPT_COOKIE, http_build_query($cookie, '', '; '));
}
if ($user != false && $pass != false) {
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "{$user}:{$pass}");
}
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeOut);
curl_setopt($curl, CURLOPT_URL, $url);
$ret = curl_exec($curl);
if (curl_errno($curl)) {
return false;
}
curl_close($curl);
if (!($split = strpos($ret, "\r\n\r\n"))) {
return $ret;
}
$data = array();
$data['header']['_header'] = substr($ret, 0, $split);
$cookie = array();
preg_match_all('#set-cookie: ([^=]*)=([^;]*)#i', $data['header']['_header'], $cookie);
if (isset($cookie[1][0])) {
foreach ($cookie[1] as $key => $Cc) {
$data['header']['cookie'][urldecode($Cc)] = urldecode(trim($cookie[2][$key]));
}
}
$location = array();
preg_match_all('#location: (.*)#i', $data['header']['_header'], $location);
if (isset($location[1][0]) && trim($location[1][0]) != '') {
$data['header']['location'] = trim($location[1][0]);
}
$length = array();
preg_match_all('#content-length: ([0-9]*)#i', $data['header']['_header'], $length);
if (isset($length[1][0]) && trim($length[1][0]) != '') {
$data['header']['length'] = $length[1][0];
}
$fileName = array();
preg_match_all('#content-disposition: attachment; *filename=["\']*([^"\']+)["\'\n\r]+#iU', $data['header']['_header'], $fileName);
if (isset($fileName[1][0]) && trim($fileName[1][0]) != '') {
$data['header']['fileName'] = $fileName[1][0];
}
if ($headerOnly == false) {
$data['body'] = @substr($ret, $split + 4);
return $data;
}
}
?>
حالا مثلاً اگه بخوایم اطلاعات موردنظر شما رو به صفحه فرضی yahoo.com/deactivate.php با روش Post ارسال کنیم و ببینیم جواب چی بوده، میتونیم مشابه مثال زیر عمل کنیم:
$post = array('email' => 'someone@yahoo.com', 'password' => '123456');
if($result = sURL('http://www.yahoo.com/deactivate.php', $post)) {
echo $result['body'];
}