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.

4

0 回答 0