1,105,423 Community Members

curl->simplexml_load_file->mysql

Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm trying to get an xml file from an ftp and then insert select data into mysql db. I'm shooting in the dark but from googling this is what i've tried so far:

   $curl = curl_init();
    $file = fopen("import.xml", 'w');
    curl_setopt($curl, CURLOPT_URL, "ftp://company@company.company.se/company/import.xml"); #input
    curl_setopt($curl, CURLOPT_FILE, $file); #output
    curl_setopt($curl, CURLOPT_USERPWD, "$_FTP[user]:$_FTP[password]");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $testcurl = curl_exec($curl);

    curl_close($curl);

    $xml = simplexml_load_file($testcurl);

foreach($xml->loanaccount as $mess){

$account_number= mysql_real_escape_string($mess->account_number);
$main= mysql_real_escape_string($mess->main);

//insert into databse                     
mysql_query("INSERT INTO account (account_number, main)
VALUES ('$account_number', '$main')")
or die(mysql_error());

echo "inserted into mysql<br /><br />";

//show updated records            
printf ("Records inserted: %d\n", mysql_affected_rows());  
}

This is the output I get:

connected to DB

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "" in /www/webvol11/kh/ezjvt3700zfmb4q/company.se/public_html/service/test1.php on line 29

Warning: Invalid argument supplied for foreach() in /www/webvol11/kh/ezjvt3700zfmb4q/company.se/public_html/service/test1.php on line 30

Member Avatar
pritaeas
mod_pritaeas
11,315 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

Can you check what $testcurl contains? I've read that there may be an issue if the FTP server requires PASV. Check some of the CURLOPT_FTP_xxx options.

Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks for your reply!

I almost got it now, however i'm wondering if it's possible to make the foreach statement conditional.

I tried to insert this $gid = $mess->account->{'tenor'};if ( $gid == "nytt" ) { after the foreach statement but to now avail.

    foreach($xml->loanaccount as $mess){
       $gid = $mess->account->{'tenor-type'};
            if ( $gid == "nytt" ) {
               $account_number= mysql_real_escape_string($mess->account_number);
               $main= mysql_real_escape_string($mess->main);
           //insert into databse                     
             mysql_query("INSERT INTO account (account_number, main)
            VALUES ('$account_number', '$main')")
         or die(mysql_error());
            echo "inserted into mysql<br /><br />";
           //show updated records            
          printf ("Records inserted: %d\n", mysql_affected_rows());  
        }
}

Any thoughts?

Member Avatar
adishardis
Junior Poster
101 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I decided to skip the if statement so all is well :)

Question Answered as of 1 Year Ago by pritaeas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: