2

我的输出是这样的

  B005CI62XY    Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - White
B008FZY5NU  Nike Men's Shox Turbo+ 13 Running Shoe
B008F9E7ZM  Nike Dual Fusion Women's Running Shoes 525752-001 Black/Fireberry/Silver
B006VXT4M0  Nike Free Run+ 2
B004PAR0N6  Nike Men's NIKE T-LITE VIII LEATHER 4E RUNNING SHOES
B005QA4X3O  Nike Mens Dual Fusion St 2 Neutral Runner Gray Sneaker
B004UTNLFS  Nike The Overplay VI Men's Basketball Shoe
B007T8HL9O  Nike Lady Free Run+ V3 Running Shoes
B004L7OH8O  Nike Men's Air Monarch IV Running
B007RJQQ7I  Nike Air Max+ 2012 Mens Running Shoes 487982-100
B004L0DAWA  Nike Women's Musique IV Dance Shoes
B004K4GNF8  Nike Men's Air Monarch IV Running
B004LBS6W8  NIKE Air Monarch IV (4E) MENS RUNNING SHOES
B0071AV6P0  Nike Air Max+ 2012 Mens Running Shoes 487982-001
B008MQ5CLQ  Nike Men's NIKE AIR VISI PRO III NBK BASKETBALL SHOES
B004TR0S8O  Nike Dri-Fit Elite Basketball Socks
B0070062M8  Nike Women's NIKE LUNARSWIFT+ 3 WMNS RUNNING SHOES
B007OX34FE  Nike Free Run+ 3 Mens Running Shoes 510642-002
B0070066R4  Nike Women's Revolution Running
B0021K13IU  Nike Men's NIKE AIR FORCE 1 '07 BASKETBALL SHOES
B004UTSMY8  Nike DART 9 Men's Running Shoe
B004ANFDU0  Nike Men's T-LITE VIII Leather Training Shoes
B003YY6RCE  Nike Men's T-Lite VIII Leather Running
B005CD4MUY  Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - Black
B007P4ZR0W  Nike Free Run+ 3 Mens Running Shoes 510642-706
B004YKIVVC  Nike Women's Dart 9 Running
B008FQCFV8  Nike Men's Dart 9 Running
B004RL0J9U  Nike Golf Men's Tech Essentials Web Belt
B0077QNU0W  Nike Women's NIKE FREE TR FIT 2 WMNS RUNNING SHOES
B003NZ0Y9G  Nike Dri-Fit Half-Cushion Crew Socks - 3 pack
B007IXAFFC  Nike Air Max Navigate LE Mens Running Shoes 456977-010
B0074SC7ZC  Nike Air Max+ 2012 Mens Running Shoes 487982-010
B003JK1UIE  Nike Men's NIKE CORTEZ BASIC NYLON '06 TRAINING SHOES
B0057FRVQY  Nike DART 9 LEATHER 443862-001 Black
B004PSS6WW  Nike Men's Crew Cut Moisture Management Socks 3 pack
B004RL0J9K  Nike Golf Mens Tech Essentials 3 Pack Belt Gift Set
B006YSPB9C  Nike Kids NIKE FREE RUN 2.0 (GS) RUNNING SHOES
B004LBWNYA  NIKE WMNS NIKE INITIATOR (WOMENS)
B007ZQG5FQ  Nike Lady Air Relentless 2 Running Shoes
B004IWW8OW  Nike 375833 Pro Bra
B007P4XKK6  Nike Free Run+ 3 Mens Running Shoes 510642-401
B006EPQ4SW  Nike One Size Fits All Web Belts : Black, Gray & Navy 3 Pack
B004LBSWI6  Nike Women's T-Lite VIII Leather Fitness
B0056IU79A  NIKE GTS '09 Canvas Sneakers Shoes White Mens
B00722V5DU  Nike Women's Free TR Twist SL
B003JK8N7A  Nike Men's NIKE CORTEZ BASIC LEATHER '06 CASUAL SHOES
B007V67N1K  Nike The Overplay VII Men's Basketball Shoe
B007ETEMWM  Nike Shox Calistra Womens Dance Pumps / Shoes - Black
B0060NPWIG  Nike Performance Athletic Crew Socks - 3 Pack White Size Large Men's shoe size 8-12 Made In USA
B0085ZD4WI  Nike Air Alvord 10
B005CD6JJG  Nike Men's Performance Moisture Wicking Crew Socks 6 Pack - Shoe Size: 8-12 Black
B007SOI26U  Nike Men's Dart 9 Running
B005844FQI  Nike Women' s Lunarglide+ 3
B007URIUTE  Nike Men's Air Relentless II Running
B007ZHLUUU  Nike Men's Air Relentless II Running
B004JVD1EI  NIKE 6.0 Mavrk Mens Shoes
B004KMC4FI  Nike Benassi Swoosh Men's Slide
B007P4VAI0  Nike Free Run+ 3 Mens Running Shoes 510642-600
B006ZBH7BS  Nike Men's Lunarfly+ 3 Running Training Shoes-Black/Gray/Blue/Green
B006T3FPMK  Nike Sweet Classic Leather
B0088CMY6U  Nike Men's NIKE MAVRK SKATE SHOES
B007W6O0AQ  Nike Air Max+ 2012 Womens Running Shoes White/Anthracite-Hot Punch-Pure Platinum 487679-103
B004LBIWA4  Nike T-Lite VIII Running Shoes White Womens
B00510KIKQ  Nike Men's Quarter Cut Moisture Management Socks 3 pack
B00785MBCU  Nike Air Max+ 2012 Mens Running Shoes 487982-104
B0071BQEII  Nike Women's NIKE FREE RUN+ 2 WMNS RUNNING SHOES
B004YKIZDG  Nike Dart 9 Men's Running Shoe
B004G83DHO  Nike Men's NIKE NIKE5 GATO LTR SOCCER INDOOR SHOES
B0072KINAU  Nike Lady Air Max+ 2012 Running Shoes
B0087G0XRE  Nike Women's Flex Trainer 2012 Running
B00767BA6S  Nike SCRAMBLER TR2 313388-072
B007Y8QL0E  Nike Air Max+ 2012 Mens Running Shoes 487982-008
B007C79DD4  Nike Free Run 2.0 GS Grey Blue Pink Flash 2012 Youth Running Shoes 477701-007
B009PQ6P2M  Nike Men's NIKE DART 9 RUNNING SHOES
B007ZTM97G  Nike Women's Zoom Vomero+ 7 Running
B003MBW6LK  Nike Men's NIKE SWEET CLASSIC LEATHER CASUAL SHOES
B008HORLC6  Nike Free Run+ 3 Men's Running Shoes
B007VCOJLG  Nike Free 4.0 V2 Dark Obsidian Blue Mens Running Shoes 511472-404
B004TN5O50  Nike Women's Musique IV Fitness
B008G1HON6  Nike Men's Shox Turbo+ 13 Running
B006U1URCE  Nike LunarEclipse+ 2
B003DKEW6M  Nike Golf Web Belt 3-Pack
B0035QSI2I  NIKE Cortez Basic Nylon (GS) Big Kid's Retro Sneaker
B005191WYI  Nike Kids' Dart 9 Running
B004LBT1NQ  Nike Air Monarch IV WIDE (4E) Mens 416355-001 Sneaker
B0065C9A0I  Nike Air Max 90 Mens Running Shoes
B0052SU5H8  Nike Brasilia 5 Medium Duffel Grip
B005HFAC6A  Nike Free Run+ 2 Women's Running Shoes Black White-Anthracite
B0073XDPTA  Nike Air Max+ 2012 Mens Running Shoes 487982-016
B00351Z7AY  Nike 269604 Pro-Core Men's Compression Shorts 6"
B009B1QA8K  Nike Free Run+ 3 Mens Running Shoes 510642-440
B007VFRFMI  Nike Free Run 3 (GS) Big Kids Running Shoes 512098-600
B0081V17PM  Nike Lunarfly 3 + Women's Running Shoes 510791 010 Grey
B0091HCGBY  Nike Women's Revolution Running
B0049P0YIU  Nike Pro Core Long Sleeve Compression Top
B008FR8FES  Nike Free Run+3 Womens Running Shoes 510643-401
B00767D71E  Nike Free Run+ 2 Mens Running Shoes 443815-012
B004LK2RHE  Nike Shox Air Lunar NZ Mens Running Shoes
B0082CTMIE  Nike Women's Flex Trainer 2
B003G7FWHU  Nike Men's NIKE CORTEZ BASIC LEATHER '06 CASUAL SHOES

这是输出的代码:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Tester</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
</head>
<body>
<?php

$search = 'Nike';
$temp_search = $search;
$category = 'Shoes';
$flag = 0;
$page = 11;
$temp = '';

for($i=1;$i<=$page;$i++){
?>
<script type="text/javascript">
    $(function(){
        $.ajax({
            url: 'search.php',
            type: 'post',
            datatype: 'json',
            data: { search: '<?php echo $search; ?>', category: '<?php echo $category; ?>', page: '<?php echo $i; ?>' },
            success:function(data){
                $('#data').append(data);
            }
        });
    });
</script>
<?php
    if($i == 10){
        $i = 1;
        $flag++;
        $search = $temp_search.' '.$flag;
    }
    if($flag == 1){
        break;
    }
}
?>
<table id="data">

</table>

</body>
</html>

这是处理并将值返回到索引页面的代码

//search.php
    <?php

include 'Amazon-ECS-PHP-Library-master/samples/sampleSettings.php';
require 'Amazon-ECS-PHP-Library-master/lib/AmazonECS.class.php';

$amazonEcs = new AmazonECS(AWS_API_KEY, AWS_API_SECRET_KEY, 'com', AWS_ASSOCIATE_TAG);
$amazonEcs->requestDelay(true);
$amazonEcs->associateTag(AWS_ASSOCIATE_TAG);

$response = $amazonEcs->responseGroup('Large')->category('Shoes')->page($_POST['page'])->search($_POST['search']);
$fh = fopen('log', 'w');
$array_item = unserialize(file_get_contents('log'));
foreach($response->Items->Item as $item){
    $array_item[] = array('ASIN'=>$item->ASIN, 'Title'=>$item->ItemAttributes->Title);
    echo '<tr><td>'.$item->ASIN.'</td><td>'.$item->ItemAttributes->Title.'</td></tr>';
}
fwrite($fh, serialize($array_item));
fclose($fh);
?>

这是查看文本文件值的代码,仅用于检查目的

 <?php
var_dump(unserialize(file_get_contents('log')));
?>

它的输出就是这样

 array (size=10)
  0 => 
    array (size=2)
      'ASIN' => string 'B005CI62XY' (length=10)
      'Title' => string 'Nike Men's Performance Moisture Wicking No Show Socks 6 Pair - Shoe Size: 8-12 - White' (length=86)
  1 => 
    array (size=2)
      'ASIN' => string 'B008FZY5NU' (length=10)
      'Title' => string 'Nike Men's Shox Turbo+ 13 Running Shoe' (length=38)
  2 => 
    array (size=2)
      'ASIN' => string 'B008F9E7ZM' (length=10)
      'Title' => string 'Nike Dual Fusion Women's Running Shoes 525752-001 Black/Fireberry/Silver' (length=72)
  3 => 
    array (size=2)
      'ASIN' => string 'B006VXT4M0' (length=10)
      'Title' => string 'Nike Free Run+ 2' (length=16)
  4 => 
    array (size=2)
      'ASIN' => string 'B004PAR0N6' (length=10)
      'Title' => string 'Nike Men's NIKE T-LITE VIII LEATHER 4E RUNNING SHOES' (length=52)
  5 => 
    array (size=2)
      'ASIN' => string 'B005QA4X3O' (length=10)
      'Title' => string 'Nike Mens Dual Fusion St 2 Neutral Runner Gray Sneaker' (length=54)
  6 => 
    array (size=2)
      'ASIN' => string 'B004UTNLFS' (length=10)
      'Title' => string 'Nike The Overplay VI Men's Basketball Shoe' (length=42)
  7 => 
    array (size=2)
      'ASIN' => string 'B007T8HL9O' (length=10)
      'Title' => string 'Nike Lady Free Run+ V3 Running Shoes' (length=36)
  8 => 
    array (size=2)
      'ASIN' => string 'B004L7OH8O' (length=10)
      'Title' => string 'Nike Men's Air Monarch IV Running' (length=33)
  9 => 
    array (size=2)
      'ASIN' => string 'B007RJQQ7I' (length=10)
      'Title' => string 'Nike Air Max+ 2012 Mens Running Shoes 487982-100' (length=48)

因为我注意到存储的数组只是索引页面中的最后 10 个项目,所以我要发生的是索引页面中的所有项目都将保存在文本文件中,我不知道如何解决这个问题,任何人有任何建议或帮助我解决问题的建议将是一个很大的帮助,谢谢!

4

4 回答 4

4

在不关心文件锁定问题的情况下,您需要移动一些语句:

// removed the fopen() because file_get_contents() already opens the file for you
$array_item = unserialize(file_get_contents('log'));

// manipulate the array here
foreach($response->Items->Item as $item){
    $array_item[] = array('ASIN'=>$item->ASIN, 'Title'=>$item->ItemAttributes->Title);
    echo '<tr><td>'.$item->ASIN.'</td><td>'.$item->ItemAttributes->Title.'</td></tr>';
}

file_put_contents('log', serialize($array_item));

这就是你所需要的。

更新

您的代码执行 11 个 AJAX 请求(针对 11 个结果页面),每个请求调用 Amazon 来填充每个页面的结果;这是非常低效的,您应该只执行一次搜索,找出有多少页面,然后使用 Amazon API 本身一次性获取所有结果。

这样做将首先消除保留临时文件的需要,因为您search.php只需一次调用即可将所有结果返回给 JavaScript。

于 2013-01-15T10:18:32.790 回答
3

我认为您需要以“附加”模式打开文件。

$fh = fopen('log', 'a');
于 2013-01-15T10:14:01.513 回答
3

我认为您的答案可能是您为“fopen”设置的模式

http://us3.php.net/manual/en/function.fopen.php

你可以看到这个函数的第二个参数是模式。您将其命名为“w”,这意味着直接来自文档:

仅供写作;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。

这将每次覆盖您的文件。您很可能希望以“a”(即附加)模式打开文件。

仅供写作;将文件指针放在文件末尾。如果该文件不存在,请尝试创建它。

于 2013-01-15T10:14:22.797 回答
3

$fh = fopen('log', 'w');将其更改为$fh = fopen('log', 'a');

w - 将删除现有内容 a- 将保留内容并附加新内容

于 2013-01-15T10:15:40.400 回答