2011-11-03 82 views
0

我试图读取rss提要并将其值存储在我的数据库中。此我使用这个存储SimpleXMLElement数据库中的对象值和图像

$homepage = file_get_contents('http://showmycode.co.in/supermob/feeds/rss/Jaipur'); 
    $movies = new SimpleXMLElement($homepage); 
echo '<pre>'; 
print_r($movies); 

,然后我发现

 SimpleXMLElement Object 

( [@属性] =>数组 ( [版] => 2.00 )

[channel] => SimpleXMLElement Object 
    (
     [title] => supermobdeal 
     [description] => best discount accross canada   
     [link] => SimpleXMLElement Object 
      (
      ) 

     [lastBuildDate] => Thu, Nov 3rd 2011, 00:32 GMT 
     [generator] => supermobdeal 
     [image] => SimpleXMLElement Object 
      (
       [url] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png 
       [title] => supermobdeal 
       [link] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png 
       [description] => Feed provided by supermobdeal 
      ) 

     [item] => Array 
      (
       [0] => SimpleXMLElement Object 
        (
         [title] => Let your 
         [link] => http://showmycode.co.in/supermob/homes/index/Jaipur/25 
         [small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/ 

         [description] => trainers r any course 



         [start_date] => 2011-10-26 
         [end_date] => 2011-11-30 
         [start_time] => 09:26:00 
         [end_time] => 09:26:50 
         [price] => 50$ 
         [value] => 199$ 
         [discount] => 74.87% 
         [city] => Jaipur 
        ) 

       [1] => SimpleXMLElement Object 
        (
         [title] => Feel a 
         [link] => http://showmycode.co.in/supermob/homes/index/Jaipur/31 
         [small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/3b8d0_Jaip_OrientSpa_24thOct_M_1.jpg 

         [description] => Orient Spa 
         [city] => Jaipur 
        ) 

       [2] => SimpleXMLElement Object 
        (
         [title] => Feel a 
         [link] => http://showmycode.co.in/supermob/homes/index/Jaipur/26 
         [small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/0a646_Bombay-Biryani1.jpg 

         [description] => Taxes 



         [city] => Jaipur 
        ) 

      ) 

    ) 

) 如何将tittle,链接或其他属性存储在我的数据库中并将图像存储在文件夹中

回答

0
//get contanints from SimpleXMLElement Object 
    foreach($movies->channel->item as $opt){ 
    $title= $opt->title; 
    $tittle=mysql_real_escape_string($title); //for Escapes special characters in a string you can use for all varriables if require 
    $link=$opt->link; 
    $des=$opt->description; 
    $image=$opt->medium_image_url; 

    //for store image from http url to my folder 
    $ch = curl_init($image); 
    $fp = fopen('images/'. time() .'.'.'jpg', 'wb');//path for store image 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_exec($ch); 
    curl_close($ch); 
     fclose($fp); 

    include('db.php');//config file for db connection 
    $sql="INSERT INTO rss (tittle, link, image,description,city) 
    VALUES ('$tittle', '$link', '$image','$des','$city')";//insert record in database table 
     $result=mysql_query($sql) or die('Error, insert query failed'); 

这个代码存储从http url.thanks ..

varriables在你的文件夹中的数据库和形象价值