0
我試圖通過API獲取XML數據,然後將該xml數據添加到數據庫中。單個API請求一次獲取50個結果,然後對於下一個50個結果,我必須根據上次獲取的結果添加page-num = 2左右。儘管while循環無法按預期工作
一切工作正常,除了做while while循環,我很困惑。採取什麼我已經嘗試了一下:
// Main Call
function call_to_cj($import_id) {
flush();
ob_flush();
do{
// Get Last Fetched Page
$Last_Import = $this->CI->import->get_last_fetched($import_id);
// Insert Product with foreach loop
$Product = $this->insert_response($this->prepare_request($import_id, $Last_Import));
// This should work after returning the $Product with
// $this->insert_response($this->prepare_request($import_id, $Last_Import));
if($Product !== FALSE) {
//$Last_Import = $Last_Import + 1;
//$this->CI->import->update_last_fetched($Last_Import, $import_id);
}
else {
exit(json_encode(array("status"=>"y")));
}
//echo $Last_Import . "<br>";
flush();
ob_flush();
sleep(3);
//exit();
} while($Product !== FALSE); // Stop if Product return FALSE
}
正如你在
if($Product !== FALSE) {
//$Last_Import = $Last_Import + 1;
//$this->CI->import->update_last_fetched($Last_Import, $import_id);
}
看到這部分應該從$Product = $this->insert_response($this->prepare_request($import_id, $Last_Import));
和insert_response()
方法所獲得的價值後,工作也有一個foreach循環。
// $data is coming through $this->prepare_request($import_id, $Last_Import)
// which is fine just a request to API and Return the XML Result
function insert_response($data) {
$attributes = $data->products->attributes();
$Records_Return = $attributes->{'records-returned'};
// Data Loop
foreach ($data->products[0] as $product) {
$p_name = strip_tags($this->clean_xml_response($product->name));
$p_description = strip_tags($this->clean_xml_response($product->description));
$p_url = urldecode($this->clean_xml_response($product->{'buy-url'}));
$p_manuf = strip_tags($this->clean_xml_response($product->{'manufacturer-name'}));
$p_image = urldecode($this->clean_xml_response($product->{'image-url'}));
$p_adid = $product->{'ad-id'};
$p_price = $product->price;
$p_saleprice = (string)$product->{'sale-price'};
$p_currency = (string)$product->currency;
$p_stock = $product->{'in-stock'};
$p_msrp = empty($product->{'retail-price'}) ? "" : $product->{'retail-price'};
$p_cat = $this->clean_xml_response($product->{'advertiser-category'});
$p_isbn = empty($product->isbn) ? "" : (string)$product->isbn;
$p_sku = empty($product->sku) ? "" : (string)$product->sku;
$p_upc = empty($product->upc) ? "" : (string)$product->upc;
$Category_Id = $this->CI->import->check_category($p_cat);
// Check Category
if($Category_Id === FALSE) {
$stmt_c = "insert into {CI}categories (category_name) "
. "values(?)";
$this->CI->db->query($stmt_c, array($p_cat));
$Category_Id = $this->CI->db->insert_id();
}
// Insert Product
$stmt_p = "insert into {CI}products (p_name, p_description, category_id, store_id, link, link_id, manf, tags, "
. "image, p_price, p_msrp, p_sale_price, p_currency, p_country, p_stock, p_isbn, p_sku, p_upc, unique_key, import_id) "
. "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$this->CI->db->query($stmt_p, array(
$p_name,
$p_description,
$Category_Id,
$Store_Id,
$p_url,
$p_adid,
$p_manuf,
'',
$p_image,
$p_price,
$p_msrp,
$p_saleprice,
$p_currency,
'',
$p_stock,
$p_isbn,
$p_sku,
$p_upc,
$unikey,
$this->import_information['import_id']
));
$Product_Id = $this->CI->db->insert_id();
}
// Through away if no more result founds
if($Records_Return < 50) {
return FALSE;
}
else {
return TRUE;
}
}
現在的問題是,我不$Last_Import
1更新1,通過上述方法得到的結果後,其不斷增加。就像我在API請求中有60個請求一樣,應該只工作2次,因爲1個API請求只返回50條記錄。
我錯過了什麼嗎?或做錯了什麼?請以正確的方式指導我。