0
<?php
header("Content-type: text/xml");

$xml = new SimpleXMLElement("<noresult>1</noresult>");

$fn = urlencode($_REQUEST['fn']);
$ln = urlencode($_REQUEST['ln']);
$co = $_REQUEST['co'];

if (empty($fn) || empty($ln)):
    echo $xml->asXML();
    exit();
endif;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://www.linkedin.com/pub/dir/?first={$fn}&last={$ln}&search=Search");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_TIMEOUT, 8);

$res = curl_exec($ch);

preg_match("/<div id=\"content\".*?<\/div>\s*<\/div>/ms", $res, $match);

if (!empty($match)):
    $dom = new DOMDocument();
    $dom->loadHTML($match[0]);

    $ol = $dom->getElementsByTagName('ol');
    $vcard = $dom->getElementsByTagName('li');

    $co_match_node = false;
    for ($i = 0; $i < $vcard->length; $i++):
        if (!empty($co) && stripos($vcard->item($i)->nodeValue, $co) !== false) $co_match_node = $vcard->item($i);
    endfor;

    if (!empty($co_match_node)):
        echo $dom->saveXML($co_match_node);
        // my idea is to put code here to save in the database.
    else:
        echo (string)$dom->saveXML($ol->item(0));
    endif;

else:
    echo $xml->asXML();
endif;

curl_close($ch);
exit();

I'm trying to save XML into a MySQL database. However, I don't know how to parse the $dom or how to segregate the "li". There are 5 fields needed in the database:

  • span.given-name
  • span.family-name
  • span.location
  • span.industry
  • dd.current-content span

These fields are available in the XML.

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Ey Jay
  • 75
  • 1
  • 8
  • Are you looking to save the XML into the database? I only see one comment. – Chris Forrence Jun 19 '13 at 18:57
  • Yes sir, i need to save the span fields name: span.given-name span.family-name span.location span.industry dd.current-content These values of these span fields that i need to save in the database. – Ey Jay Jun 19 '13 at 19:00
  • `$xml = $dom->saveXML($node)`, then $xml is just a plaintext string you stuff into the database. – Marc B Jun 19 '13 at 19:07
  • `if (!empty($co_match_node)): echo $dom->saveXML($co_match_node); else: echo $dom->saveXML($ol->item(0)); endif; ` Where will i put it there? Sorry, my skills is way above the person that create this script, although i understand how the script works, i just can't seem to edit to suit my needs. – Ey Jay Jun 19 '13 at 19:15
  • possible duplicate of [Parsing and processing HTML/XML?](http://stackoverflow.com/questions/3577641/parsing-and-processing-html-xml) - We don't know as well which problems you have with just parsing it, so please keep it to a programming question. It does not require that much code btw, please keep needless details out of your *working* example. E.g. just provide the minimum set of HTML in form of a string instead of showing the curl and outline where exactly you hit the road-block. Try to re-formulate your question. Selecting HTMl by class? See the linked duplicate for libraries that do that. – hakre Jun 20 '13 at 07:59
  • The thing is, i want to parse the $xml that is echoed in the if-else statement. the value there is what i want to be inserted in the mysql database. – Ey Jay Jun 20 '13 at 15:09

0 Answers0