title: PHP + Wikipedia: Get content from the first paragraph in a Wikipedia article?
body: <p>I’m trying to use Wikipedia’s API (api.php) to get the content of a Wikipedia article provided by a link (like: <a href="http://en.wikipedia.org/wiki/Stackoverflow" rel="nofollow">http://en.wikipedia.org/wiki/Stackoverflow</a>).
  And what I want is to get the first paragraph (which in the example of the Stackoverflow wiki article is: <em>Stack Overflow is a website part of the Stack Exchange network[2][3] featuring questions and answers on a wide range of topics in computer programming.[4][5][6]</em>).</p>
  
  <p>I’m going to do some data manipulation with it.</p>
  
  <p>I’ve tried with the API url: <a href="http://en.wikipedia.org/w/api.php?action=parse&amp;page=Stackoverflow&amp;format=xml" rel="nofollow">http://en.wikipedia.org/w/api.php?action=parse&amp;page=Stackoverflow&amp;format=xml</a> but it gives me some kind of error. It outputs:</p>
  
  <pre><code>&lt;api&gt;
  &lt;parse displaytitle="Stackoverflow" revid="289948401"&gt;
  &lt;text xml:space="preserve"&gt;
  &lt;ol&gt; &lt;li&gt;REDIRECT &lt;a href="/wiki/Stack_Overflow" title="Stack Overflow"&gt;Stack Overflow&lt;/a&gt;&lt;/li&gt; &lt;/ol&gt; &lt;!-- NewPP limit report Preprocessor node count: 1/1000000 Post-expand include size: 0/2048000 bytes Template argument size: 0/2048000 bytes Expensive parser function count: 0/500 --&gt; &lt;!-- Saved in parser cache with key enwiki:pcache:idhash:21772484-0!*!0!!*!* and timestamp 20110525165333 --&gt;
  &lt;/text&gt;
  &lt;langlinks/&gt;
  &lt;categories/&gt;
  &lt;links&gt;
  &lt;pl ns="0" exists="" xml:space="preserve"&gt;Stack Overflow&lt;/pl&gt;
  &lt;/links&gt;
  &lt;templates/&gt;
  &lt;images/&gt;
  &lt;externallinks/&gt;
  &lt;sections/&gt;
  &lt;/parse&gt;
  &lt;/api&gt;
  </code></pre>
  
  <p>I found this snippet of code that I’ve tried</p>
  
  <pre><code>$doc = new DOMDocument();
  $doc-&gt;loadHTML($wikiPage);
  $xpath = new DOMXpath($doc);
  $nlPNodes = $xpath-&gt;query('//div[@id="bodyContent"]/p');
  $nFirstP = $nlPNodes-&gt;item(0);
  $sFirstP = $doc-&gt;saveXML($nFirstP);
  echo $sFirstP; 
  </code></pre>
  
  <p>but I can’t get the HTML content in the variable $wikiPage.</p>
  
  <p>I do not know if this is the best or most optimal way to do it so please feel free to comment on that and otherwise any suggestion or solutions would be very appreciated.</p>
  
  <p>Thank you<br>
  - Mestika</p>