dsfb20227 2013-03-01 19:51 采纳率: 0%
浏览 33


I am having an almost identical problem to this one, posted in '09. It doesn't appear to be successfully solved, and I have been poring over related questions all day & implementing suggestions I have found.

I am attempting to pull dynamic (changes daily) data from an XML source (not a file) and insert into a mySQL database. When I attempt to pull the data without fwrite, the data returns without tags/keys in the output. Thus, I cannot properly assign the data to specific database fields.

I decided to write the information to a file and go from there, but the file aborts/"completes" at 334kb every single time. Unfortunately, I am a cURL novice and don't have the chops to see exactly what the issue is. Also, I am using vqMod for OpenCart to do this, otherwise I'd be using straight up PHP.

What am I missing?

Also, is there a way to pull the xml as an array instead of as a string (thereby potentially bypassing the fwrite step)? Or, should I be writing to some other file type?

Any suggestions or a redirect to a solved question I've missed are appreciated.

This is the relevant code, commented portions are fixes I've attempted:

$curl = curl_init();
    $fp = fopen('dir/file.xml' , "w" );
    //w+ will not download any information from the url - file is created but empty.

    //AS IS, downloads first 334KB of file then lands on a blank page
    //and a 500 error when any of these other options are implemented. 
    curl_setopt($curl, CURLOPT_URL, 'http://www.url.com');
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

    //  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //  curl_setopt($curl, CURLOPT_TIMEOUT,  300);
    //  curl_setopt($curl, CURLOPT_NOPROGRESS, false);
    //  curl_setopt($curl, CURLOPT_RANGE, '0-1000');

    //  $data = array();
    $data = curl_exec($curl);

    fwrite($fp, $data);


Update: Attempted to use simplexml_load_string instead of fwrite() to pull one product's information, but am still having limited success. Example of XML I am using:

        <?xml version="1.0"?>
                  <![CDATA[Mfr Part No]]>
                  <![CDATA[Special Code 1]]>
            <![CDATA[GROUP NAME]]>

This is an example of the $data return for multiple products when I do not use fwrite(): 0 N 75 1.02 Y Y N 0 N 294 475 0 N 288 12 Y Y Y 18 Y 222 456 3786

  • 写回答

2条回答 默认 最新

  • douqin0676 2013-03-05 22:19

    I got it to work sans fwrite() using this code:

                        $curl = curl_init();
                        curl_setopt($curl, CURLOPT_URL, 'http://www.url.com');
                        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
                        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
                        curl_setopt($curl, CURLOPT_NOPROGRESS, FALSE);
                        curl_setopt($curl, CURLOPT_MAXCONNECTS, 1);
                        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); 
                        $newparts = curl_exec($curl);
                        $xml = simplexml_load_string($newparts);
                    foreach($xml->modpart as $item){
                          if(($item->active == 'Y') || ($item->active == 'YES')){
                              $status = '1';
                          } else {
                              $status = '0';
                          $rs   = $this->db->query("SELECT manufacturer_id FROM ".DB_PREFIX."manufacturer WHERE name = '".$item->mfr->mfr_name."'");
                          $mfr_id = $rs->row['manufacturer_id'];
                              if($item->codes->code->fieldname == 'Mfr Part No'){
                                  $mpn = $item->codes->code->fieldvalue;
                          $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product (model, sku, mpn, quantity, date_available, sort_order, manufacturer_id, price, weight, status, date_added) 
                              VALUES ( 
                                   NOW(), 1000,
                        $pr = $this->db->query("SELECT product_id FROM ".DB_PREFIX."product WHERE sku = '".$item->part."'");
                        $product_id = $pr->row['product_id'];
                                  $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_description (product_id, language_id, name, meta_keyword, meta_description, description, tag)
                                                  '" . (int)$product_id . "',
                                                  '" . $item->part. "',
                                                  '" . $item->part. "',
                                $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_to_store SET product_id = '" . (int)$product_id . "', store_id = '0'");
                                $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "url_alias SET query = 'product_id=" . (int)$product_id . "', keyword = '" .$item->part . "'");
                            foreach($item->cats as $cats){
                                $category = $cats->cat;
                                    foreach($category as $category_id){
                                    $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_to_category SET product_id = '" . (int)$product_id . "', category_id = '" . (int)$category_id . "'");

    I still have had no luck with the timeout issue, but at least I can get my products into the database by some means. I will post the timeout solution if/when I find it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应