0

我遇到了与09 年发布的这个问题几乎相同的问题。它似乎没有成功解决,我整天都在研究相关问题并实施我发现的建议。

我正在尝试从 XML 源(不是文件)中提取动态(每天更改)数据并插入到 mySQL 数据库中。当我尝试在没有 fwrite 的情况下提取数据时,数据返回时没有输出中的标签/键。因此,我无法正确地将数据分配给特定的数据库字段。

我决定将信息写入一个文件并从那里开始,但文件每次都以 334kb 的大小中止/“完成”。不幸的是,我是一个 cURL 新手,没有能力确切地了解问题所在。此外,我正在使用 vqMod for OpenCart 来执行此操作,否则我将直接使用 PHP。

我错过了什么?

此外,有没有办法将 xml 作为数组而不是字符串拉出(从而可能绕过 fwrite 步骤)?或者,我应该写入其他文件类型吗?

感谢任何建议或重定向到我错过的已解决问题。

这是相关代码,注释部分是我尝试过的修复:

$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);

    curl_close($curl);
    fclose($fp);

更新:尝试使用 simplexml_load_string 而不是 fwrite() 来提取一个产品的信息,但仍然取得了有限的成功。我正在使用的 XML 示例:

        <?xml version="1.0"?>
      <response>
        <root>
          <part>
            <![CDATA[PARTNUM]]>
          </part>
          <errorcode>0</errorcode>
          <errormsg></errormsg>
          <special>N</special>
          <description>
            <![CDATA[]]>
          </description>
          <price>75</price>
          <weight>1.02</weight>
          <webpurch>Y</webpurch>
          <altnum>
            <![CDATA[ALT-NUM]]>
          </altnum>
          <active>Y</active>
          <redo>
            <![CDATA[]]>
          </redo>
          <codes> 
              <code>
                <fieldname>
                  <![CDATA[Mfr Part No]]>
                </fieldname>
                <fieldvalue>
                  <![CDATA[PARTNUM]]>
                </fieldvalue>
               </code>
               <code>
                <fieldname>
                  <![CDATA[Special Code 1]]>
                </fieldname>
                <fieldvalue>
                  <![CDATA[XYZ123]]>
                </fieldvalue>
               </code> 
            </codes>
          <customtag>N</customtag>
          <onhand>0</onhand>
          <notes>
            <![CDATA[PRODUCT-SPECIFIC NOTE]]>
          </notes>
          <mfr>
            <mfr_name>
              <![CDATA[MFR]]>
            </mfr_name>
          </mfr>
          <altpartnums>
            <altnum>
              <![CDATA[PARTNUM.12]]>
            </altnum>
          </altpartnums>
          <gtrue>N</gtrue>
          <group>
            <![CDATA[GROUP NAME]]>
          </group>
          <categories>
            <cat>294</cat>
            <cat>475</cat>
          </categories>
        </root>
      </response>

这是我不使用 fwrite()时多个产品的 $data 返回示例:0 N 75 1.02 Y Y N 0 N 294 475 0 N 288 12 Y Y 是 18 是 222 456 3786

4

2 回答 2

0

删除这一行:

fwrite($fp, $data);

我很确定$data它是空的,因为您已指定不返回任何字符串数据,而是将其写入文件。文件也是空的也就不足为奇了。

于 2013-03-01T20:26:12.207 回答
0

我使用以下代码让它在没有 fwrite() 的情况下工作:

        if(extension_loaded('curl')){
                    $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';
                      }


                if(!empty($item->mfr)){ 
                      $rs   = $this->db->query("SELECT manufacturer_id FROM ".DB_PREFIX."manufacturer WHERE name = '".$item->mfr->mfr_name."'");
                      $mfr_id = $rs->row['manufacturer_id'];
                }

                 if(!empty($item->codes)){
                          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 ( 
                              '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
                              '".mysql_real_escape_string($item->sku)."', 
                               '".$mpn."', 
                              '".mysql_real_escape_string($item->mpn)."', 
                               NOW(), 1000,
                              '".$mfr_id."',
                               '".mysql_real_escape_string($item->price)."', 
                               '".mysql_real_escape_string($item->weight)."', 
                               '".$status."', 
                                 NOW() 
                              )");    

                    $pr = $this->db->query("SELECT product_id FROM ".DB_PREFIX."product WHERE sku = '".$item->part."'");
                    $product_id = $pr->row['product_id'];

                    if(isset($product_id)){

                              $this->db->query("INSERT IGNORE INTO " . DB_PREFIX . "product_description (product_id, language_id, name, meta_keyword, meta_description, description, tag)
                                      VALUES(                                                       
                                              '" . (int)$product_id . "',
                                              '1',
                                              '" . $item->part. "',
                                              '" . $item->part. "',
                                              '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
                                              '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."', 
                                              '".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."' 
                                      )");


                            $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 . "'");
                                } 
                        } 
                             $this->cache->delete('product');
                    } 
            } 

            curl_close($curl);

    } 

我仍然没有解决超时问题,但至少我可以通过某种方式将我的产品放入数据库。如果/当我找到它时,我会发布超时解决方案。

于 2013-03-05T22:19:54.650 回答