Home >Backend Development >PHP Tutorial >How to correctly implement PHP to obtain blog data_PHP tutorial

How to correctly implement PHP to obtain blog data_PHP tutorial

WBOY
WBOYOriginal
2016-07-22 09:01:441147browse

Currently many websites provide free personal blog services, such as Google, Sina, NetEase, etc. How to make full use of free blogs requires us to use them During the process, we continue to summarize and think about it. For programmers, how to use PHP to obtain Blogger blog RSS or Atom data is very important. Here I will briefly introduce to you the basic method of using PHP to obtain Blogger blog RSS or Atom data. With PHP Get Google's Blogger blog data as an example to understand the basic principles of PHP getting RSS or Atom data for reference.

Prerequisites for using PHP to obtain blog data

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

PHP get blog data example code

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">blogUrl</span><span> = </span><span class="attribute-value">'http://shifen.blogspot.<br>com/feeds/posts/default'</span><span>;   </span></span></li>
<li>
<span>$</span><span class="attribute">atom</span><span> = </span><span class="attribute-value">simplexml_load_file</span><span> ( $blogUrl );   </span>
</li>
<li class="alt">
<span>$atom-</span><span class="tag">></span><span>registerXPathNamespace (<br> 'atom', 'http://www.w3.org/2005/Atom' );   </span>
</li>
<li>
<span>$</span><span class="attribute">title</span><span> = $atom-</span><span class="tag">></span><span>title;   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">subtitle</span><span> = $atom-</span><span class="tag">></span><span>subtitle;   </span>
</li>
<li>
<span>$</span><span class="attribute">blogFeeds</span><span> = $atom-</span><span class="tag">></span><span>link [0] [href];   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">blogURL</span><span> = $atom-</span><span class="tag">></span><span>link [2] [href];   </span>
</li>
<li>
<span>$</span><span class="attribute">blogNextURL</span><span> = $atom-</span><span class="tag">></span><span>link [3] [href];   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">entrys</span><span> = $atom-</span><span class="tag">></span><span>xpath ( '//atom:entry' );  </span>
</li>
</ol>

PHP code analysis for obtaining blog data

1. Define the blog blogger address, such as: $blogUrl = 'http://shifen.blogspot.com/feeds/posts/default' ;

2. Use PHP's built-in simplexml_load_file function to convert the blogger's XML data into objects.

Related knowledge about simplexml_load_file (see PHP manual for details)
Description: simplexml_load_file loads an XML document into an object.
Prototype: simplexml_load_file ( filename [,class_name [,options [, ns [, is_prefix]]]] )

3. Use PHP’s built-in registerXPathNamespace function to create a namespace context for the next XPath query. Combined with the previous simplexml_load_file function, it supports providing namespace. Blogger's namespace uses http://www.w3.org/2005/Atom, which is convenient for calling Blogger's RSS or Atom data.

4. Get Blogger’s RSS or Atom data.

(1) Get the Blogger blog space title, such as: $atom->title, return: very happy
(2) Get the Blogger blog space subtitle, such as: $atom->subtitle, Return: It’s always good to learn something and it can make you very happy!
(3) Get the Blogger blog RSS address, such as: $atom->link [0] [href], return: http://shifen.blogspot.com/feeds/posts/default
(4) Get the Blogger blog URL address, such as: $atom->link [2] [href], return: http://shifen.blogspot.com/
(5) Get the next page address of the Blogger blog RSS, such as :$atom->link [3] [href], return: http://shifen.blogspot.com/feeds/posts/default?start-index=26&max-results=25
(6) Get Blogger blog Article content, such as: $atom->xpath ( '//atom:entry' ), returns an array of articles, defaulting to the latest 25 articles.

As can be seen from the above example of PHP getting blog data, PHP can easily achieve it by using the two built-in functions simplexml_load_file and registerXPathNamespace to get Blogger blog RSS or Atom data.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445915.htmlTechArticleCurrently many websites provide free personal blog services, such as Google, Sina, NetEase, etc. How to use a free blog To make full use of it, we need to constantly summarize and think about it during use...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn