douliaopan1419 2017-06-11 04:45
浏览 74


This php script below fetches XML job feed from the remote url and map the fields into the Database. The script was working fine, till yesterday when the specialty node values from the XML feed were changed to multiple values separated with comma.



    if(isset($_POST["submit"])) {
        $ch = curl_init($filename);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        if($output === false) throw new Exception('Curl error: ' . curl_error($ch),__LINE__);

        $xml = simplexml_load_string($output);

        if($xml) {
            $hmacct = HPP_ACCT;
            $db = db_career();
            $sql = "select sp_code,sp_name from specialties";
            $res = $db->query($sql);
            if( !$res ) 
                throw new Exception(DEBUG?"$db->error : $sql":'Problem with Specialties Table',__LINE__);
            $hamspec = array();
            while( list($spc,$sph) = $res->fetch_row() ) {
                $hamspec[$sph] = $spc; 
            $sql = "select st_code,st_name from states";
            $res = $db->query($sql);
            if( !$res ) 
                throw new Exception(DEBUG?"$db->error : $sql":'Problem with States Table',__LINE__);
            $hamstadt = array();
            while( list($spc,$sph) = $res->fetch_row() ) { 
                $hamstadt[$sph] = $spc; 

                $sql = "delete from opportunities where o_acct='".HPP_ACCT."' AND status=1 ";
                $res = $db->query($sql);
            $client = new Customer($db,HPP_UID);

            $states = array(
                    'New Hampshire'=>'NH',
                    'New Jersey'=>'NJ',
                    'New Mexico'=>'NM',
                    'New York'=>'NY',
                    'North Carolina'=>'NC',
                    'North Dakota'=>'ND',
                    'Rhode Island'=>'RI',
                    'South Carolina'=>'SC',
                    'South Dakota'=>'SD',
                    'West Virginia'=>'WV',

            $ar = array();

            foreach($xml->Opportunity as $job) {
                $jid = "JOB {" .addslashes($job->OpportunityId)."}";
                $jobid = "(JOB ID " .addslashes($job->JobNumber).")";
                $facility = addslashes($job->facility)." ".$jobid;
                $city = $job->city;
                $state = $states["$job->state"];
                $spec = addslashes(trim($job->specialty));

                switch($spec) {
                    case "Family Medicine":
                        $spec="Family Practice";
                    case "Internal Medicine":
                        $spec="Internal Medicine";
                    case "Pediatric General":
                        $spec="Pediatric - General";

                $spec = trim($spec);
                    @$spec2 = $spec == 'Emergency Medicine'?'EM':($spec == 'Hospitalist Medicine'?'HOS':$hamspec["$spec"]);

                }catch(Exception $e){
                    echo $e->getMessage().' ('.$e->getCode().')<br>';
                    echo "No specialty mapping for ".$spec.". Will be skipped.<br/>";
                } else {
                        $title = $spec2." at ".$facility;
                        $description = html_entity_decode(addslashes($job->description));
                        $contact = $job->recruiter_name;
                        $phone = $job->RecruiterPhone;
                        $email = $job->recruiter_email;
                        $ldescr = html_entity_decode(addslashes($job->facil_description));
                        $acity = addslashes($city); $astate = addslashes($state);
                        $sql = "select l_id from locations where l_acct = $client->acct and status = 1 and l_facility = '$facility' and l_city = '$acity' and l_state = '$astate'";

                        $res = $db->query($sql);
                        if( !$res ) 
                            throw new Exception(DEBUG?"$db->error : $sql":'Problem with Locations Table',__LINE__);
                        if( !$res->num_rows ) { 
                            // create new location
                            $cdescr = html_entity_decode(addslashes($job->city_description));
                            $sql ="insert into locations (l_facility,l_city,l_state,l_uid,l_acct,l_description,l_commdescr,exp_date) values('$facility','$acity','$astate',$client->uid,$client->acct,'$ldescr','$cdescr',ADDDATE(NOW(), INTERVAL 1 YEAR))";
                            $result = $db->query($sql);
                            if( !$result ) 
                                throw new Exception(DEBUG?"$db->error: $sql":'Can not insert locations',__LINE__);
                            $locid = $db->insert_id;
                        } else 
                            list($locid) = $res->fetch_row();
                    //create Opportunity
                    @$opp = new Opportunity($db,0,$locid,$client->uid,$client->acct);
                    $opp->o_name = $jid;
                    $opp->o_city = ($acity);
                    $opp->o_state = ($astate);
                    $opp->specialty = $spec2;
                    $opp->o_facility = $facility;
                    $opp->description = $description;
                    $opp->exp_date = $client->exp_date;
                    $opp->practice_type = $spec2 == 'HOS'?'Hosp':($spec == 'EM'?'ER':'');
                    $opp->o_email = addslashes($email);
                    $opp->o_phone = addslashes($phone);
                    $opp->o_contact = addslashes(htmlspecialchars( strip_tags($contact)));

                    $sql = "insert into importrac (jobid,jobacct,jobflag,jobopp) VALUES ('$jid',$client->acct,0,$opp->id) ON DUPLICATE KEY UPDATE jobflag=0";


            print "jobs added!";
<h1>XML Import</h1>
<form action="import.php" method="post" enctype="multipart/form-data">
<input type="submit" name="submit" value="Run">

Below is the example of the current XML feed with multiple specialties:

       <headline>Jobs in Los Angeles, CA</headline>
       <specialty>Family Medicine, Internal Medicine, Pediatric - General</specialty>
       <city>San Fernando Valley, CA</city>
       <facility>Medical Center</facility>
       <RecruiterPhone>(503) 555-333</RecruiterPhone>

If there is only one specialty value from the XML feed, php script will map it properly: <specialty>Family Medicine</specialty>.

Is it possible to extract multiple specialty values from the XML specialty node above and map it with the current PHP/MySQL code?


Even if I use explode function, couple of jobs were not imported. Please see the list below.

<specialty>Family Medicine, Geriatric Medicine</specialty>
<specialty>Medical Oncology, Surgery - Breast</specialty>
<specialty>Emergency Medicine, Family Medicine</specialty>
<specialty>Internal Medicine, Pediatric - General</specialty>
<specialty>Emergency Medicine, Family Medicine</specialty>
<specialty>Orthopedics - General, Orthopedics - Sports Medicine</specialty>
<specialty>Family Medicine, Urgent Care/Immediate Care</specialty>
<specialty>Family Medicine, Internal Medicine, Pediatric - General</specialty>
<specialty>Family Medicine, Sports Medicine</specialty>
<specialty>Cardiology, Cardiology - Adv Heart Failure/Transplant</specialty>
<specialty>Cardiology, Cardiology - Non-Invasive</specialty>
<specialty>Pediatric - Developmental/Behavioral, Pediatric – Neurodevelopment Disabilities
<specialty>Pediatric - Critical Care Medicine, Pediatric - Intensivist
<specialty>Family Medicine, Internal Medicine, Urgent Care/Immediate Care
<specialty>Family Medicine, Urgent Care/Immediate Care</specialty>
  • 写回答

1条回答 默认 最新

  • douzhi7082 2017-06-11 06:36

    When you fetch the speciality, you can split it using explode to get the separate parts...

    $specList = explode(',' addslashes(trim($job->specialty)));

    Now $specList will be an array of specialities (if there is only one it will be in $specList[0].

    You then can loop around this array, processing each one at a time...

    for ( $specList as $spec )    {
        switch($spec) {
                        case "Family Medicine":
    // The rest of the code that deals with specialities here
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥25 annaconda jvpyter
  • ¥20 来一个会抓包app支付接口的
  • ¥30 vivado2017.4的sdk打不开
  • ¥50 matlab,文献复现
  • ¥20 寻找Flink CDC指导,有偿咨询
  • ¥15 cruise如何仿真不同温度下车辆的经济性?
  • ¥20 扑克牌游戏的完整代码
  • ¥88 求帮安装Python架构的vnpy
  • ¥20 逻辑回归法分析品牌知名度和购买意向
  • ¥15 华为云主机-异常连接