繁体   English   中英

php file_get_contents($url) 返回奇怪的字符串

[英]php file_get_contents($url) return strange string

我尝试从网站获取新闻

http://www.science-support.ru/news.html

,但我无法从网页内容中提取任何内容。 当我应用file_get_contents()我得到其他函数不起作用的字符串。 最初,我尝试使用simple_html_dom.php file_get_html() ,但是此源中的其他函数无法识别获取的 DOM 中的任何元素。

关于其他功能:

<?php
$content = file_get_contents('http://www.science-support.ru/news.html'); //normal page
$content = substr($content,20); //strange characters
$content_arr = explode( 'div id="box3"' , $content ); //doesn't work

echo $content;
echo $content_arr[0];
?>

在 substr() 之后我得到类似的东西

" <�/tr> <�/table> <�/div> <�div id="box3"><�!-- InstanceBeginEditable name="page-content" --> <�h4 class="yellow">14.11.2014 />2>AB8 $>=40/<�/h4> <�p>1JO2;O=K @57C;LB0BK :>=:C@A0 =0 ?@8AC645=85 ?>8A:>2KE 3@0=B>2 ?> @>3@0<<5 =0CG=>-B5E=8G5A:>9 <>45@=870F88 8 ?>2KH5=8N :20;8D8:0F88 <>;>4KE CG5=KE >AA88 2014-2015 33.<�a href="news/news2014-nota-res.html" class="txt" >?>4@>1=55<�/a><�/p>"

如何提取可读的内容? 谢谢

尝试从 Curl 执行此操作,我发布了一些可以帮助您的代码

<?php

  if( isset( $_POST['site_url'] ) && !empty( $_POST['site_url'] ) ){
    echo get_html($_POST['site_url']);
  } else {
echo 'false';
  }

  function get_html($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
   }

  ?>

从您的 html 表单中获取网站 url 这是 html 如何设置

<div class="col-md-offset-2 col-md-8">
            <form role="form" id="siteform" method="post">
                <div class="form-group">
                    <input type="url" class="form-control" name="site_url" id="site_url" placeholder="Enter your site address">
                    <span class="help-block"></span>
                </div>
                <button data-loading-text="Please wait..." type="submit" id="url_getter" class="btn btn-default btn-success">Submit</button>
            </form>
        </div>
    </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM