将curl的XML结果插入数据库

时间:2017-10-26 08:56:01

标签: php xml curl

我试图将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>

1 个答案:

答案 0 :(得分:-1)

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

首先,您使用$xmldoc -> load($data);load()期待文件名并传递实际数据。所以将其更改为loadXML()

然后当您访问数据的元素时,您的使用 ->nodeValue->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;
}