Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:03:23+00:00 2026-06-05T06:03:23+00:00

Possible Duplicate: Getting content using wikipedia API Using PHP, how do I get the

  • 0

Possible Duplicate:
Getting content using wikipedia API
Using PHP, how do I get the first paragraph of a Wikipedia article with the MediaWiki API?

This is mainly an XML-related question.

I’m trying to do this using the MediaWiki API.

I’ve managed to get a response in XML format (can change to JSON if easier), and I see all the content I need in the response. Example:

http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=War%20and%20Peace&prop=revisions&rvprop=content&format=xmlfm

I used xmlfm here for formatting reasons. In PHP I’m doing:

$request = "http://en.wikipedia.org/w/api.php?format=xml&action=query&titles=War%20and%20Peace&prop=revisions&rvprop=content&format=xml";

$response = @file_get_contents($request);

$wxml = simplexml_load_string($response);

var_dump($wxml);

Which prints out everything in the XML. My question is, how do I get the first paragraph out of this?

I can parse it from the full article, so basically what I’m asking is, how do I get the article text from this XML? Of course, if there’s a way to go for the first paragraph directly, that would be best.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:03:25+00:00Added an answer on June 5, 2026 at 6:03 am

    I’d definitely say you’re looking for this.

    If you want to retrieve everything in the first section (not just the first paragraph):

    // action=parse: get parsed text
    // page=Baseball: from the page Baseball
    // format=json: in json format
    // prop=text: send the text content of the article
    // section=0: top content of the page
    
    $url = 'http://en.wikipedia.org/w/api.php?action=parse&page=Baseball&format=json&prop=text&section=0';
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked)
    $c = curl_exec($ch);
    
    $json = json_decode($c);
    
    $content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML)
    
    // pattern for first match of a paragraph
    $pattern = '#<p>(.*?)</p>#s'; // http://www.phpbuilder.com/board/showthread.php?t=10352690
    if(preg_match_all($pattern, $content, $matches))
    {
        // print $matches[0]; // content of the first paragraph (including wrapping <p> tag)
        print strip_tags(implode("\n\n",$matches[1])); // Content of the first paragraph without the HTML tags.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: getting identity row value using ADO.NET Entity This is my example code:
Possible Duplicate: Getting DIV content with Regular Expression Let me first tell you that
Possible Duplicate: Getting the screen resolution using PHP well the question is self explanatory
Possible Duplicate: NoClassDefFoundError - Eclipse and Android I'm seeing this question is getting asked
Possible Duplicate: Getting the Specified content into the buffer in c I have a
Possible Duplicate: Get Variable from httpcontext coming from NSUrlRequest This question will be a
Possible Duplicate: Using GetHashCode for getting Enum int value Is it safe to use
Possible Duplicate: Getting the ID of the element that fired an event using jQuery
Possible Duplicate: # in url getting ignored in php 5.2 I have following link
Possible Duplicate: How to Get time difference in iPhone I´m getting date and time

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.