Quick tips on how PHP crawls Web content

Source: Internet
Author: User
Tags php language

How can php crawl the Web content correctly? This problem is a bit difficult for a friend who is in contact with the PHP language soon. The class has introduced a concrete solution for you today.


First of all, in the c\windows in the php.ini I opened the Extension=php_curl.dll function, and then also restarted the Apapche, the following is the PHP crawl I wrote the content of the crawl Baidu PHP information:

< PHP

Initialize Curl

$ch = Curl_init () or Die (Curl_error ());

echo "Test";

Set URL parameters

curl_setopt ($ch, Curlopt_url, "http://http://www.baidu.com/s?wd=php");

Require Curl to return data

curl_setopt ($ch, curlopt_returntransfer,1);

Execute request

$result = curl_exec ($ch) or Die (Curl_error ());

Gets the returned result and displays

echo $result;

echo Curl_error ($ch);

Turn off Curl

Curl_close ($ch);

?>

Why does PHP not respond when crawling Web content? Even the text of the test is not, if I put echo "test", put in the first line can be output, I guess the curl_init () function is not running!

You see PHP phpinfo () with no curl extension support!

Cuff Php_curl.dll to C:\windows\ and C:\Windows\System32 and try again after you restart Apache.

Not php_curl.dll This file, is the PHP directory in the libeay32.dll,ssleay32.dll copy to C:\Windows\System32 inside restart Apache

For the sake of server security, so turn off the allow_url_fopen.

When the server Allow_url_fopen = Off, the file_get_contents cannot be used, only when set on.

< PHP/*

$getstr =file_get_contents ("http://www. 163.com/weatherxml/54511.xml ");

$QX =explode ("\" ", Strstr ($getstr," qx= "));

$WD =explode ("\" ", Strstr ($getstr," wd= "));

$qximg =explode ("\" ", Strstr ($getstr," qximg= "));

$qximg _=explode (",", $qximg [1]);

echo "Beijing". $qx [1]. "";

echo $WD [1];*/

echo "< img src= ' http://news. 163.com/img/logo/". $qximg _[0]." ' > < img src= ' http://news.163.com/img/logo/". $qximg _[1]." ' > ";

?>

The following example of PHP crawling Web content is the pass Curl_init function to get 163 weather forecast

Remove the php.ini (; extension=php_curl.dll) in front of the (;)) Save

Copy Php_curl.dll,libeay32.dll,ssleay32.dll to C:\Windows\System32, restart IIS, no Apache

< PHP

Initialize Curl

$ch = Curl_init () or Die (Curl_error ());

Set URL parameters

curl_setopt ($ch, Curlopt_url, "http://http://www.163.com/weatherxml/54511.xml");

Require Curl to return data

curl_setopt ($ch, curlopt_returntransfer,1);

Execute request

$result = curl_exec ($ch) or Die (Curl_error ());

Gets the returned result and displays

echo $result;

echo Curl_error ($ch);

$QX =explode ("\" ", Strstr ($result," qx= "));

$WD =explode ("\" ", Strstr ($result," wd= "));

$qximg =explode ("\" ", Strstr ($result," qximg= "));

$qximg _=explode (",", $qximg [1]);

echo "Beijing". $qx [1]. " < br/> ";

echo $WD [1];

Turn off Curl

Curl_close ($ch);

?>

Through the above on the PHP crawl Web content learning, you can actually work on their own to deepen the understanding of it. More information: http://www.kokojia.com/s64/


  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.