[英]php file_get_contents($url) return strange string
我嘗試從網站獲取新聞
,但我無法從網頁內容中提取任何內容。 當我應用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.