PHP为什么我的代码没有进入第20行的foreach循环?文件\u get\u contents()似乎不起作用
此屏幕截图显示URL存储在$URL中 这个屏幕截图显示,在我向代码中添加echo$html之后,它表示未定义的变量$url和file_get_contents():filename不能为空 此外,我还尝试了stackoverflow上的几乎所有内容,包括file_get_html()和cURL。似乎什么都不管用。请告诉我哪里出了问题PHP为什么我的代码没有进入第20行的foreach循环?文件\u get\u contents()似乎不起作用,php,file-get-contents,domparser,Php,File Get Contents,Domparser,此屏幕截图显示URL存储在$URL中 这个屏幕截图显示,在我向代码中添加echo$html之后,它表示未定义的变量$url和file_get_contents():filename不能为空 此外,我还尝试了stackoverflow上的几乎所有内容,包括file_get_html()和cURL。似乎什么都不管用。请告诉我哪里出了问题 <?php include_once('simple_html_dom.php'); $base_url = "https://www.instagram
<?php
include_once('simple_html_dom.php');
$base_url = "https://www.instagram.com/";
$html = "";
if ( isset($_POST['username']) ) {
$url = $base_url.htmlspecialchars($_POST['username'])."/";//concatenate $base_url to username to generate full URL
}
$html = file_get_contents($url); //access the URL in $url
$doc = new DOMDocument;
$doc->loadHTML($html); //get HTML of the webpage given by file_get_contents
$tags = $doc->getElementsByTagName('img');
$arr = (array)$tags;
if (empty($arr)) {
echo 'emptyarray';
}
foreach ($tags as $tag) {
echo $tag->getAttribute('src');
}
?>
编辑:
如果使用“http://stackoverflow.com/questions”而不是“https://www.instagram.com/its_kushal_here”文件,则“获取内容”(get_contents())工作正常且未失败。刷新页面时,是否确保将post参数传递到新请求 问题似乎就在这里
if ( isset($_POST['username']) ) {
$url = $base_url.htmlspecialchars($_POST['username'])."/";
}
如果未设置
$\u POST['username']
,则不会定义$url
。同时从@$doc->loadHTML($html)中删除@
代码>以便您可以看到它输出的错误。这将帮助您解决在这一点之后失败的问题。如果(isset($\u POST['username']){$url=$base\u url.htmlspecialchars($\u POST['username'])。“/”;}
此段未成功表示您的帖子未成功1-删除所有测试代码:“echo$url…”有很多测试代码,使您更难看到发生了什么------2-包含“simple\u dom\u html”文件------3-尝试使用codepen包含代码段(4)考虑使用评论来显示你正在尝试什么do@MASIDDIQUI即使我也这么认为。但是,第一张截图会告诉你这篇文章是成功的。@Cason注意到并采取了行动。谢谢你。如果你发现了什么问题,请告诉我。所以很简单..访问instagram.com/its_kushal_here/查看源代码并找到img标签…你呢???Kirsty Wright。是的,我确保设置了POST参数。我没有刷新页面。相反,我再次填写了上一页的表单,以便所有POST参数都得到它们的值。这是我在删除“@”后得到的额外值。警告:DOMDocument::loadHTML():在第12行的C:\xampp\htdocs\instaDP\php\process.php中作为输入提供的空字符串