How to correctly implement PHP to obtain blog Data _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
How to correctly implement PHP to obtain blog data. At present, many websites provide free personal blog services, such as Google, Sina, and NetEase. to make full use of free blogs, we need to constantly sum up and think about it during use.

Currently, many websites provide free personal blog services, such as Google, Sina, and NetEase. we need to constantly summarize and think about how to make full use of free blogs, for programmers, it is very important to use PHP to obtain Blogger Blog RSS or Atom data. here we will briefly introduce the basic methods for getting blogger Blog RSS or Atom data using PHP, take PHP to obtain google's Blogger blog data as an example. learn about the basic principles of getting RSS or Atom data from PHP for your reference.

Prerequisites for using PHP to retrieve blog Data

There is a free space for Google Blogger.
Get RSS or Atom address http://shifen.blogspot.com/feeds/posts/default for free space

PHP obtains the blog data instance code

 
 
  1. $blogUrl = 'http://shifen.blogspot.
    com/feeds/posts/default';
  2. $atom = simplexml_load_file ( $blogUrl );
  3. $atom->registerXPathNamespace (
    'atom', 'http://www.w3.org/2005/Atom' );
  4. $title = $atom->title;
  5. $subtitle = $atom->subtitle;
  6. $blogFeeds = $atom->link [0] [href];
  7. $blogURL = $atom->link [2] [href];
  8. $blogNextURL = $atom->link [3] [href];
  9. $entrys = $atom->xpath ( '//atom:entry' );

PHP obtains blog Data code analysis

1. define the blogger address of the blog, for example, $ blogUrl = 'http: // shifen.blogspot.com/feeds/posts/default ';

2. use the PHP built-in simplexml_load_file function to convert the XML data of blogger into an object.

Simplexml_load_file (for details, refer to the PHP Manual)
Note: simplexml_load_file loads an XML file into an object.
Prototype: simplexml_load_file (filename [, class_name [, options [, ns [, is_prefix])

3. use the PHP built-in registerXPathNamespace function to create a namespace context for the next XPath query. Combined with the simplexml_load_file function, the namespace can be provided. the Blogger namespace uses http://www.w3.org/2005/atom, which facilitates the use of BLOGGER'S rssor atomdata.

4. obtain the RSS or Atom data of Blogger.

(1) get the title of the Blogger blog space, for example, $ atom-> title. return: very pleasant.
(2) get the title of the Blogger blog space, for example, $ atom-> subtitle. return: learning is always good, which makes you very happy!
(3) get Blogger Blog RSS address, such as: $ atom-> link [0] [href], return: http://shifen.blogspot.com/feeds/posts/default
(4) get Blogger blog URL, such as: $ atom-> link [2] [href], return: http://shifen.blogspot.com/
(5) get the next page address of Blogger Blog RSS, such as: $ atom-> link [3] [href], return: http://shifen.blogspot.com/feeds/posts/default? Start-index = 26 & max-results = 25
(6) obtain the content of a Blogger blog, for example, $ atom-> xpath ('// atom: entry'), and return the array of articles. by default, the latest 25 articles are published.

The above PHP blog Data acquisition instance shows that PHP can easily obtain Blogger Blog RSS or Atom data using simplexml_load_file and registerXPathNamespace built-in functions.


...

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.