我正在尝试通过API获取XML数据,然后将该xml数据添加到数据库中。单个API请求一次获取50个结果,然后对于接下来的50个结果,我必须根据上次提取的内容添加page-num = 2等。
一切正常,除了我迷惑的do 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个请求时,do while应该只工作2次,因为1个API请求只返回50个记录。
我错过了什么吗?或做错了什么?请以正确的方式指导我。
答案 0 :(得分:0)
在查看源代码的这一部分时:
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);
我注意到在执行任何刷新之前页面正在退出。 因此,最后一个有效的产品信息可能会被跳过/不输出 在完成操作之前页面退出。
我可能会建议:
flush();
ob_flush();
if( $Product !== FALSE ) {
//echo $Last_Import . "<br>";
//$Last_Import = $Last_Import + 1;
//$this->CI->import->update_last_fetched($Last_Import, $import_id);
}
else {
exit( json_encode(array("status"=>"y")) );
}
sleep(3);