Home >Backend Development >PHP Tutorial >Sharing tips on crawling web content with PHP_PHP Tutorial
How to implement it correctlyBut why does PHP not respond after crawling the web content? There is no test text. If I put echo "test"; on the first line, it can be output. I guess the curl_init() function has not been run yet!
See if there is CURL extension support in PHP's phpinfo()!
Copy php_curl.dll to c:windows and c:windowssystem32, restart apache and try again
It is not the file php_curl.dll, but copy libeay32.dll and ssleay32.dll in the php directory to c:windowssystem32 and restart apache
For the sake of server security, allow_url_fopen is turned off.
When the server allow_url_fopen = Off, file_get_contents cannot be used. It can only be used when it is set to ON.
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php /* </span></span></li><li><span>$</span><span class="attribute">getstr</span><span>=</span><span class="attribute-value">file_get_contents</span><span>("http://www.<br />163.com/weatherxml/54511.xml"); </span></li><li class="alt"><span>$</span><span class="attribute">qx</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($getstr,"</span><span class="attribute">qx</span><span>=")); </span></li><li><span>$</span><span class="attribute">wd</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($getstr,"</span><span class="attribute">wd</span><span>=")); </span></li><li class="alt"><span>$</span><span class="attribute">qximg</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($getstr,"</span><span class="attribute">qximg</span><span>=")); </span></li><li><span>$</span><span class="attribute">qximg_</span><span>=</span><span class="attribute-value">explode</span><span>(",",$qximg[1]); </span></li><li class="alt"><span>echo "北京 ".$qx[1].""; </span></li><li><span>echo $wd[1];*/ </span></li><li class="alt"><span>//echo "</span><span class="tag"><</span><span> </span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">'http://news.<br />163.com/img/logo/".$qximg_[0]."'</span><span class="tag">><br></span><span class="tag"><</span><span> </span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">'http://news.163.com<br />/img/logo/".$qximg_[1]."'</span><span class="tag">></span><span>"; </span></span></li> <li> <span class="tag">?></span><span> </span> </li> </ol>
The following example of PHP crawling web content is to obtain the 163 weather forecast through the curl_init function
Put php.ini ( ;extension=php_curl.dll ) Remove the previous (;) and save
Copy php_curl.dll, libeay32.dll, ssleay32.dll to c:windowssystem32 and restart IIS. Apache is not installed
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span> ?php </span></span></li><li><span>//初始化curl </span></li><li class="alt"><span>$</span><span class="attribute">ch</span><span> = </span><span class="attribute-value">curl_init</span><span>() or die (curl_error()); </span></li><li><span>//设置URL参数 </span></li><li class="alt"><span>curl_setopt($ch,CURLOPT_URL,"http:<br />//www.163.com/weatherxml/54511.xml"); </span></li><li><span>//要求CURL返回数据 </span></li><li class="alt"><span>curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); </span></li><li><span>//执行请求 </span></li><li class="alt"><span>$</span><span class="attribute">result</span><span> = </span><span class="attribute-value">curl_exec</span><span>($ch) or die (curl_error()); </span></li><li><span>//取得返回的结果,并显示 </span></li><li class="alt"><span>//echo $result; </span></li><li><span>// echo curl_error($ch); </span></li><li class="alt"><span>$</span><span class="attribute">qx</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($result,"</span><span class="attribute">qx</span><span>=")); </span></li><li><span>$</span><span class="attribute">wd</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($result,"</span><span class="attribute">wd</span><span>=")); </span></li><li class="alt"><span>$</span><span class="attribute">qximg</span><span>=</span><span class="attribute-value">explode</span><span>(""",strstr($result,"</span><span class="attribute">qximg</span><span>=")); </span></li><li><span>$</span><span class="attribute">qximg_</span><span>=</span><span class="attribute-value">explode</span><span>(",",$qximg[1]); </span></li><li class="alt"><span>echo "北京 ".$qx[1]."</span><span class="tag"><</span><span> </span><span class="tag-name">br</span><span> </span><span class="tag">/></span><span>"; </span></span></li> <li><span>echo $wd[1]; </span></li> <li class="alt"><span>//关闭CURL </span></li> <li><span>curl_close($ch); </span></li> <li class="alt"> <span class="tag">?></span><span> </span> </li> </ol>
Through the above study of PHP crawling web content, you can practice it yourself and deepen your understanding of it.