尝试获取 Instagram 嵌入页面 HTML 代码时出错
Error when trying to get Instagram Embed page HTML code
我正在尝试为我的 API 获取 Instagram 嵌入页面的 HTML 代码,但 returns 我遇到了一个奇怪的错误,我不知道该怎么做现在,因为我是 PHP 的新手。该代码适用于其他网站。
我已经在 apple.com 等其他网站上尝试过了,奇怪的是,当我在 'normal' post 页面上调用此函数时它起作用了,错误仅出现在我在 '/embed' URL.
上调用它
这是我的PHP代码:
<?php
if (isset($_GET['url'])) {
$filename = $_GET['url'];
$file = file_get_contents($filename);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($file);
libxml_use_internal_errors(false);
$bodies = $dom->getElementsByTagName('body');
assert($bodies->length === 1);
$body = $bodies->item(0);
for ($i = 0; $i < $body->children->length; $i++) {
$body->remove($body->children->item($i));
}
$stringbody = $dom->saveHTML($body);
echo $stringbody;
}
?>
我这样调用 API:
https://api.com/get-website-body.php?url=http://instagr.am/p/BoLVWplBVFb/embed
我的目标是获取网站的正文,就像我在 https://apple.com URL 上调用此代码时一样。
如果您使用 CURL 并且它比 file_get_content 更快,您可以使用直接 url 来 抓取 数据。这是不同 url 的 curl 代码,这将 单独抓取 正文数据。
if (isset($_GET['url'])) {
// $website_url = 'https://www.instagram.com/instagram/?__a=1';
// $website_url = 'https://apple.com';
// $website_url = $_GET['url'];
$website_url = 'http://instagr.am/p/BoLVWplBVFb/embed';
$curl = curl_init();
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_REFERER, $website_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0(Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/66.0');
$str = curl_exec($curl);
curl_close($curl);
$json = json_decode($str, true);
print_r($str); // Just taking tha page as it is
// Taking body part alone and play as your wish
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($str);
libxml_use_internal_errors(false);
$bodies = $dom->getElementsByTagName('body');
foreach ($bodies as $key => $value) {
print_r($value);// You will all content of body here
}
}
注意:这里你不想使用https://api.com/get-website-body.php?url=....
我正在尝试为我的 API 获取 Instagram 嵌入页面的 HTML 代码,但 returns 我遇到了一个奇怪的错误,我不知道该怎么做现在,因为我是 PHP 的新手。该代码适用于其他网站。
我已经在 apple.com 等其他网站上尝试过了,奇怪的是,当我在 'normal' post 页面上调用此函数时它起作用了,错误仅出现在我在 '/embed' URL.
上调用它这是我的PHP代码:
<?php
if (isset($_GET['url'])) {
$filename = $_GET['url'];
$file = file_get_contents($filename);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($file);
libxml_use_internal_errors(false);
$bodies = $dom->getElementsByTagName('body');
assert($bodies->length === 1);
$body = $bodies->item(0);
for ($i = 0; $i < $body->children->length; $i++) {
$body->remove($body->children->item($i));
}
$stringbody = $dom->saveHTML($body);
echo $stringbody;
}
?>
我这样调用 API:
https://api.com/get-website-body.php?url=http://instagr.am/p/BoLVWplBVFb/embed
我的目标是获取网站的正文,就像我在 https://apple.com URL 上调用此代码时一样。
如果您使用 CURL 并且它比 file_get_content 更快,您可以使用直接 url 来 抓取 数据。这是不同 url 的 curl 代码,这将 单独抓取 正文数据。
if (isset($_GET['url'])) {
// $website_url = 'https://www.instagram.com/instagram/?__a=1';
// $website_url = 'https://apple.com';
// $website_url = $_GET['url'];
$website_url = 'http://instagr.am/p/BoLVWplBVFb/embed';
$curl = curl_init();
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $website_url);
curl_setopt($curl, CURLOPT_REFERER, $website_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0(Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/66.0');
$str = curl_exec($curl);
curl_close($curl);
$json = json_decode($str, true);
print_r($str); // Just taking tha page as it is
// Taking body part alone and play as your wish
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($str);
libxml_use_internal_errors(false);
$bodies = $dom->getElementsByTagName('body');
foreach ($bodies as $key => $value) {
print_r($value);// You will all content of body here
}
}
注意:这里你不想使用https://api.com/get-website-body.php?url=....