【问题标题】:Insert XML result from curl to database将来自 curl 的 XML 结果插入数据库
【发布时间】:2017-10-26 08:56:01
【问题描述】:

我正在尝试将来自 curl 的 xml 结果插入到数据库中,但我遇到了数据无法进入数据库的问题。我也尝试过mysqli方法和PDO,但数据没有进入数据库。谁能帮我解决这个问题。这是我的代码:-

MYSQLI

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url."?".$queryString);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$parameters);
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
// Save response to the variable $data
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
// Close Curl connection
if ($data === False) {
    echo "cURL ERROR:".curl_error($ch);
}
curl_close($ch);
echo $url."?".$queryString;
echo '<pre>';
echo htmlspecialchars(print_r($data, true));
echo '</pre>';
$xml= simplexml_load_string($data);
//print_r($xml) ;
con = mysqli_connect("localhost","root","");
mysqli_select_db($con,"test") or die(mysqli_connect_error());
foreach($xml -> Products as $product) {
   $product->Product;
   {
        $name = mysqli_real_escape_string($product->name);
        $short_description = mysqli_real_escape_string($product-
                             >short_description);
        $brand = mysqli_real_escape_string($product->brand);
        $model = mysqli_real_escape_string($product->model);
        $warranty_type =mysqli_real_escape_string($product->warranty_type);

         $sql = "Insert into test (name, short_description, 
                   brand,model,warranty_type ) " . "Values 
         ('$name','$short_description','$brand','$model','$warranty_type')";
          $result = mysqli_query($con, $sql);
          error_log($sql);
          if (!$result) {
          echo 'Error:' . mysqli_error($con);
          } else {
           echo 'Success';
         }
     }
 }

**PDO**
$db= new PDO('mysql:host=localhost;dbname=test','root','');
$xmldoc= new DOMDocument();
$xmldoc -> load($data);

$xmldata = $xmldoc ->getElementsByTagName('Product');
$xmlcount = $xmldata ->length;

for($i=0; $i< $xmlcount; $i++){
$name = $xmldata -> item($i) ->getElementsByTagName('name')->item(0)-> 
childNodes->item(0)->nodeValue->item(0);
$short_description = $xmldata -> item($i) -
>getElementsByTagName('short_description')->item(0)-> childNodes->item(0)-
>nodeValue->item(0);
$brand = $xmldata -> item($i) ->getElementsByTagName('brand')->item(0)-> 
childNodes->item(0)->nodeValue->item(0);
$model = $xmldata -> item($i) ->getElementsByTagName('model')->item(0)-> 
childNodes->item(0)->nodeValue->item(0);
$warranty_type = $xmldata -> item($i) -
>getElementsByTagName('warranty_type')->item(0)-> childNodes->item(0)-
>nodeValue->item(0);
$stmt = $db->prepare("insert into test values(?,?,?,?,?)");
$stmt -> bindParam(1,$name);
$stmt -> bindParam(2,$short_description);
$stmt -> bindParam(3,$brand);
$stmt -> bindParam(4,$model);
$stmt -> bindParam(5,$warranty_type);
$stmt ->execute();

}

PDO 方法显示在 DOMNode 中找不到 getElementsByTagName。我使用 PHPStorm 2016.2.1 作为我的 IDE。


这是我从 curl 得到的 xml 格式的响应

<SuccessResponse>
  <Head>
   <RequestId>0a1530d415089888818591159e</RequestId>
   <RequestAction>GetProducts</RequestAction>
   <ResponseType>Product</ResponseType>
   <Timestamp>2017-10-26T03:34:40+0000</Timestamp>
   <isES>true</isES>
</Head>
 <Body>
   <TotalProducts>589</TotalProducts>
    <Products>
    <Product>
    <PrimaryCategory>10001164</PrimaryCategory>
    <SPUId/>
    <Attributes>
      <name>Big Rabbit Furry Key Chain (Light Blue)</name>
      <short_description>&lt;ul&gt; &lt;li&gt;&lt;span style="font-
   family:arial,sans-serif; font-size:10pt"&gt;A perfect gift for yourself 
  or someone special&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="font-
 family:arial,sans-serif; font-size:10pt"&gt;Good accessory for handbag, 
 tote backpack cellphones, keychain or car&lt;/span&gt;&lt;/li&gt; 
 &lt;li&gt;&lt;span style="color:rgb(17, 17, 17); font-family:arial,sans-
 serif; font-size:10pt"&gt;Super cute and soft&lt;/span&gt;&lt;/li&gt; 
 &lt;li&gt;&lt;span style="color:rgb(17, 17, 17); font-family:arial,sans-
 serif; font-size:10pt"&gt;Ideal companion of your keys, bags, cellphones 
 car or other wonderful objects&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span 
 style="color:black; font-family:arial,sans-serif; font-
 size:10pt"&gt;Size(cm): 10 x 6 x 18&lt;/span&gt;&lt;/li&gt; &lt;/ul&gt;
 </short_description>
      <video/>
      <services/>
      <brand>Not Specified</brand>
      <model>bulu15-lightblue</model>
      <color_family/>
      <Hazmat/>
      <warranty_type>No Warranty</warranty_type>
      <warranty/>
      <product_warranty/>
      <name_ms>Big Rabbit Furry Key Chain (Light Blue)</name_ms>
      <product_warranty_en/>
      <description_ms/>
      <external_url/>
    </Attributes>
    <Skus>
      <Sku>
        <Status>active</Status>
        <quantity>100</quantity>
        <tax_class>default</tax_class>
        <_compatible_variation_>...</_compatible_variation_>
        <SellerSku>test112</SellerSku>
        <ShopSku/>
        <package_content>&lt;p&gt;1 x Big Rabbit Furry Key Chain (Light Blue)&lt;/p&gt;</package_content>
        <Url/>
        <package_width>6.00</package_width>
        <package_height>18.00</package_height>
        <special_price>0.0</special_price>
        <price>0.0</price>
        <package_length>10.00</package_length>
        <package_weight>0.10</package_weight>
        <Available>100</Available>
        <Images>
          <Image>https://my-live.slatic.net/original/601531319cd1dca721c70637e96cc403.jpg</Image>
          <Image>https://my-live.slatic.net/original/7e6ad47bbfa1de474fe36c20b06c35f7.jpg</Image>
          <Image>https://my-live.slatic.net/original/02bc2102d6864a867c25328724f0c7c7.jpg</Image>
          <Image/>
          <Image/>
          <Image/>
          <Image/>
          <Image/>
        </Images>
      </Sku>
    </Skus>
  </Product>

【问题讨论】:

  • 你能展示你从 curl ($data) 得到的结果吗
  • api.sellercenter.lazada.com.my?Action=GetProducts&Format=XM… 是我从 curl 获得的响应 url,它放在 $data 中)
  • 这给了我 XML 错误 - E3:Timestamp 已过期,您能否在您的问题中添加至少部分数据副本。
  • 我从 lazada api 获取 url,它以 xml 格式显示产品列表
  • 那么您能否将一些 XML 数据添加到您的问题中!

标签: php xml curl


【解决方案1】:

您的 PDO 版本的代码存在一些问题。

首先是您使用$xmldoc -&gt; load($data);load() 需要一个文件名并且您传入实际数据。所以把它改成loadXML()

然后,当您访问数据的元素时,您使用 -&gt;nodeValue-&gt;item(0)nodeValue 为您提供节点的内容,因此您需要在此停止。在下面的代码中,我刚刚更改了名称,但您应该看到需要如何通过您的代码进行更改。

$xmldoc= new DOMDocument();
$xmldoc -> loadXML($data);

$xmldata = $xmldoc ->getElementsByTagName('Product');
$xmlcount = $xmldata ->length;

for($i=0; $i< $xmlcount; $i++){
    $name = $xmldata -> item($i) ->getElementsByTagName('name')->item(0)->
        childNodes->item(0)->nodeValue;

        echo $name;
}

【讨论】:

  • 会对为什么这被否决感兴趣。很高兴从我的错误中吸取教训,但不确定它们是什么,因为没有反馈。
  • 感谢您的更改。我尝试将其添加到我的代码中,但出现此错误 致命错误:未捕获的错误:调用 C:\xampp\htdocs\qijang\new.php:223 中的字符串上的成员函数 item() 堆栈跟踪:#0 {main } 在第 223 行的 C:\xampp\htdocs\qijang\new.php 中抛出
  • 好的 - 你能显示第 223 行是什么吗?如果不知道导致错误的原因,就很难提供帮助。
  • 实际上在第 223 行只有一个闭合的花括号。
  • 您是否每次访问 XML 时都进行了更改,我说在 -&gt;nodeValue 部分之后不要放置任何内容?我只展示了一个例子,剩下的你需要做!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多