Я просто хочу использовать "file_get_html" для получения содержимого определенных страниц, но этот контент доступен только для пользователя loggin, есть ли способ отправить cookie, чтобы целевой сайт знал, что к странице обращается вход в систему.
require_once 'simple_html_dom.php';
$opts = array("Cookie: __qca=P0-1170249003-1395413811270"); //__qca=P0-1170249003-1395413811270
$current_url = 'http://abc.xyz';
// But it will be redirect to
$url = 'http://www.blogger.com/blogin.g?blogspotURL=http://abc.xyz'
$context = stream_context_create($opts);
$html = file_get_html($url, FALSE, $context);
echo $html;
Я делаю что-то подобное, но это не работает. Как я могу сделать это с помощью Curl? Благодарю.
$ch = curl_init(); // your curl instance
curl_setopt_array($ch, [CURLOPT_URL => "http://www.blogger.com/blogin.g?blogspotURL=http://abc.xyz", CURLOPT_COOKIE => "__qca=P0-1170249003-1395413811270"], CURLOPT_RETURNTRANSFER => true]);
$result = curl_exec($ch); // request result
$html = new simple_html_dom(); // create new parser instance
$html->load($result); // load and parse previous result
В этом примере я использовал curl_setopt_array()
для установки различных параметров curl_setopt()
вместо вызова curl_setopt()
для каждого из них.
CURLOPT_URL
устанавливает целевой URL-адрес, CURLOPT_COOKIE
устанавливает CURLOPT_COOKIE
файлов cookie, если есть несколько файлов cookie, тогда они должны быть разделены точкой с запятой, за которой следует пробел, и, наконец, CURLOPT_RETURNTRANSFER
сообщает CURLOPT_RETURNTRANSFER
чтобы вернуть ответ сервера как строку.
curl_exec()
выполняет запрос и возвращает его результат.
Затем мы создаем экземпляр simple_html_dom
и загружаем в него предыдущий результат.
Большое вам спасибо @andre, вчера вечером я провел весь вечер, чтобы найти решение: сначала мы делаем curl exe для входа в аккаунт google. и сохраните файл cookie в текстовом файле (экзамен: "/tmp/cookie.txt"), и в следующий раз все, что нам нужно сделать, - это получить содержимое файла cookie в этом файле, чтобы получить удаленный контент.
<?php
require_once 'simple_html_dom.php';
// Construct an HTTP POST request
$clientlogin_url = "https://www.google.com/accounts/ClientLogin";
$clientlogin_post = array(
"accountType" => "HOSTED_OR_GOOGLE",
"Email" => "[email protected]",
"Passwd" => "yourpasswd",
"service" => "blogger",
"source" => "your application name"
);
// Initialize the curl object
$curl = curl_init($clientlogin_url);
// Set some options (some for SHTTP)
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
// Execute
$response = curl_exec($curl);
echo $response;
// Get the Auth string and save it
preg_match("/Auth=([a-z0-9_\-]+)/i", $response, $matches);
$auth = $matches[1];
echo "The auth string is: " . $auth;
// Include the Auth string in the headers
// Together with the API version being used
$headers = array(
"Authorization: GoogleLogin auth=" . $auth,
"GData-Version: 3.0",
);
$url = 'http://testlink.html';
$curl = curl_init();
// Make the request
curl_setopt($curl, CURLOPT_URL, $url );
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
$html = new simple_html_dom(); // Create new parser instance
$html->load($response);
foreach($html->find('img') as $img) {
//echo $img->src . '</br>';
}