1

I am trying to add the option tag from a ppa, as to show on my own website the current Ubuntu distro's it support.

This is what i have so far.

<?php include 'https://launchpad.net/~gregory-hainaut/+archive/pcsx2.official.ppa#field.series'; ?>

this is the content of the option tag:

<select onchange="updateSeries(this);" size="1" name="field.series" id="field.series">
<option value="YOUR_UBUNTU_VERSION_HERE" selected="selected">Choose your Ubuntu version</option>
<option value="trusty">Trusty (14.04)</option>
<option value="saucy">Saucy (13.10)</option>
<option value="raring">Raring (13.04)</option>
<option value="quantal">Quantal (12.10)</option>
<option value="precise">Precise (12.04)</option>
<option value="lucid">Lucid (10.04)</option>
</select>
blade19899
  • 727
  • 1
  • 8
  • 32
  • Are you trying to include an external website's HTML code onto your own? Or arr you trying to submit a form from your website to theirs? – SyntaxLAMP Jan 26 '14 at 17:24
  • @SyntaxLAMP, I am trying to include an external websites HTML code onto my own! – blade19899 Jan 26 '14 at 17:25
  • don't try `include`... you want to try `file_get_contents` or `curl` to get the HTML and then use DOMDocument to parse it and get the section you want. – Ben D Jan 26 '14 at 17:37
  • Are you doing this because the code will change overtime? Otherwise why not just put it in yours manually? – SyntaxLAMP Jan 26 '14 at 17:42
  • @SyntaxLAMP,i have a website, - tutorials and such - which in I use allot of this ppa. Doing it manually, can cost a allot of time. – blade19899 Jan 26 '14 at 17:44
  • @BenD, i tried the following: `` but that just added the entire launchpad code to my website. `then use DOMDocument to parse it and get the section you want.` .... How do i do that? – blade19899 Jan 26 '14 at 17:45
  • @blade19899 - I posted an answer below with details – Ben D Jan 26 '14 at 17:59

1 Answers1

2

You'll want to use file_get_contents (or curl) to get the actual source code, and the parse it using (for instance) DOMDocument. For instance, you might try something like this:

Fist define a function that lets us get the innerHTML of a node (thank you to Hiam):

function DOMinnerHTML(DOMNode $element) 
    { 
        $innerHTML = ""; 
        $children  = $element->childNodes;

        foreach ($children as $child) 
        { 
            $innerHTML .= $element->ownerDocument->saveHTML($child);
        }

        return $innerHTML; 
    } 

Then get the contents and grab the select menu by ID

$ppa = file_get_contents('https://launchpad.net/~gregory-hainaut/+archive/pcsx2.official.ppa#field.series');

$doc = new DomDocument;
$doc->loadHTML($ppa);
$innerHTML = DOMinnerHTML($doc->getElementById('field.series_filter'));

If I echo $innerHTML the result is:

<option value="">Any series</option>
<option value="trusty">Trusty</option>
<option value="saucy">Saucy</option>
<option value="raring">Raring</option>
<option value="quantal">Quantal</option>
<option value="precise">Precise</option>
<option value="lucid">Lucid</option>

From that result, you'll notice I only got the inner html of the select menu, so you'll want to wrap the returned innerHTML in a select tag:

<select name='[your name]' [...other properties]>
<?=$innerHTML;?>
</select>

EDITS

The OP mentioned in comments that everything is working, but he wants a different select menu than the one selected above. Because the page he is scraping has invalid markup (two separate select menus have the same id) the DOMDocument getElementById call is not fetching the correct node. To correct this you have to look at the DOM tree and fine a unique parent element so that you can first grab the parent node and then run a query to find the item you're looking for In this case, the menu that the OP wants is inside of a div with an ID of "", so all we do is grab that node, and then use getElementsByTagName to grab the select menu:

//... get the code (set in $ppa) as per the original section
$doc->loadHTML($ppa);

//Grab the parent div because it has a unique ID
$parent_div = $doc->getElementById('series-widget-div');
//then seach for all <select> tags and grab the first one
$select_menu = $parent_div->getElementsByTagName('select')->item(0);
//... and now we're ready to get the innerHTML
$innerHTML = DOMinnerHTML( $select_menu );
//echo it out!
echo $innerHTML;
Community
  • 1
  • 1
Ben D
  • 14,321
  • 3
  • 45
  • 59
  • I tried your code, but it gets rendered as text? when looked at with firebug, there are tags surrounding it? see: **[screenshot](http://i.imgur.com/uVfoC0i.png)** same when viewing the source-code – blade19899 Jan 26 '14 at 19:11
  • That's because you're not wrapping the options in a `` – Ben D Jan 26 '14 at 20:34
  • Put [that](http://stackoverflow.com/questions/21366651/add-the-option-and-select-from-an-external-website-to-my-website/21367115?noredirect=1#comment32223379_21367115) comment in to your answer, and I will accept it. Tomorrow. Goodnight! – blade19899 Jan 26 '14 at 20:53
  • First off, its working great. But, i kinda wanted to keep the `(14.04)` etc. [This](http://i.imgur.com/GAHFarM.png) is how it looks now and [this](http://i.imgur.com/vFsQYtT.png) is how i am trying to get. Can't figure out how you removed the `(14.04)` etc part in the first place. The numbers are to sum more recognizable. – blade19899 Jan 31 '14 at 22:12
  • @blade19899 - I've updated the answer to address this. Basically, the site you're scraping has two items with the same ID (i.e. invalid markup), so you have to sort of work around the faulty DOM. I'm shown you how in the updated answer. – Ben D Feb 01 '14 at 06:01