2013-02-28 52 views
0

下面的脚本显然使用了http://www.hasoffers.com/wiki/Offer:create中记录的API。问题有(至少)两个部分:a)如何在一个数组中存储多个数据集。 b)该API是否接受它....array只选取要存储的最后一个值


当我运行它只存储里面“数据”我怎样才能得到它在一次存储更多数据的最后一个值的脚本?

下面的代码有例如2个值。一个是LOLO,另一个是LELE。 输出仅显示值LELE。

这是代码。

<?php 
header('Content-type: application/json'); 
$base = 'http://api.hasoffers.com/Api?'; 

$params = array(
'Format' => 'json' 
,'Target' => 'Offer' 
,'Method' => 'create' 
,'Service' => 'HasOffers' 
,'Version' => 2 
,'NetworkId' => 'demo' 
,'NetworkToken' => '....' 
,'data' => array(
     'name' => 'LOLO' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 

     ,'name' => 'LELE' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 
) 
); 

$url = $base . http_build_query($params); 

$result = file_get_contents($url); 


print_r(json_decode($result)); 
?> 

,这是输出

[request] => stdClass Object 
    (
     [Target] => Offer 
     [Format] => json 
     [Service] => HasOffers 
     [Version] => 2 
     [Method] => create 
     [NetworkId] => demo 
     [NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF 
     [data] => stdClass Object 
      (
       [name] => LELE 
       [description] => test 
       [offer_url] => http://google.nl 
       [preview_url] => http://google.nl 
       [expiration_date] => 08-08-2013 
      ) 

    ) 
+0

你有只有一个元素的数组,里面的键值重复,所以最后一个被记住。 – 2013-02-28 09:07:41

+0

但是当我运行脚本它显示我[0] => ...和[1] => ...我怎么能做到这一点,而没有得到这个值因为Hasoffer平台的API只接受'data'=> [名称] ..不是'data'=> [0] => [name] – 2013-02-28 09:21:56

+0

答案可能是(并且已经浏览了我认为是的api文档):您必须发送两个单独的请求,两个创建两个单独的提议。 – VolkerK 2013-02-28 09:33:19

回答

0

这是我做的,它的作品

<?php 

// Bestand openen 
if (($file = fopen("test2.csv", "r")) !== FALSE) { 
    // Eerste rij van Excel als value gebruiken 
    $header = fgetcsv($file, 1000, ";"); 


    // Een loop door Excel file 
    while (($data = fgetcsv($file, 1000, ";")) !== FALSE) { 



      // combineer de eerste rij met de gegevens 
      $combined = array_combine($header,$data); 

      // Connectie maken met Hasoffers bij elke waarden 
      $base = 'http://api.hasoffers.com/Api?'; 

      $params = array(
       'Format' => 'json' 
       ,'Target' => 'Offer' 
       ,'Method' => 'create' 
       ,'Service' => 'HasOffers' 
       ,'Version' => 2 
       ,'NetworkId' => 'demo' 
       ,'NetworkToken' => '.....' 
       ,'data' => $combined 
      ); 

      $url = $base . http_build_query($params); 

      $result = file_get_contents($url); 

      // Tijdelijk printen 
      print_r(json_encode($result)); 
    } 
} 
?> 

我创建包括CSV文件的循环。 问题在于它只与hasoffer连接一次,并且只允许一个值。

0
'data' => array(
    array (
    'name' => 'LOLO', 
    'description' => 'test', 
    'offer_url' => 'http://google.nl', 
    'preview_url' => 'http://google.nl' 
    'expiration_date' => '08-08-2013'), 
    array (
    'name' => 'LELE', 
    'description' => 'test', 
    'offer_url' => 'http://google.nl', 
    'preview_url' => 'http://google.nl', 
    'expiration_date' => '08-08-2013')) 
+0

您是否知道这与http://www.hasoffers.com/wiki/Offer:create一起使用还是通用答案? – VolkerK 2013-02-28 09:12:29

+0

@VolkerK只是一般的答案 – 2013-02-28 09:54:26

0
,'data' => array(array(
     'name' => 'LOLO' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 
     ), 
     array(
     ,'name' => 'LELE' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 
     ) 
) 

您需要将其添加为一个多维数组否则将会用相同的密钥元素。请注意,array (....) ADDD里面你array

array(array(

+0

你知道这与http://www.hasoffers.com/wiki/Offer:create一起工作,还是一个通用的答案? – VolkerK 2013-02-28 09:12:08

+0

这是一个通用的答案,你需要在php – 2013-02-28 09:13:58

0

或者,你可以将它存储在这样

$data = array(); 
$data[0] = array('name' => 'LELE' 
    , 'description' => 'test' 
    , 'offer_url' => 'http://google.nl' 
    , 'preview_url' => 'http://google.nl' 
    , 'expiration_date' => '08-08-2013' 
); 

$data[1] = array('name' => 'LOLO' 
    , 'description' => 'test' 
    , 'offer_url' => 'http://google.nl' 
    , 'preview_url' => 'http://google.nl' 
    , 'expiration_date' => '08-08-2013' 
); 

$params = array(
    'Format' => 'json' 
    , 'Target' => 'Offer' 
    , 'Method' => 'create' 
    , 'Service' => 'HasOffers' 
    , 'Version' => 2 
    , 'NetworkId' => 'demo' 
    , 'NetworkToken' => 'NETU2nzMw8AYS6EGgjFrjGR88GcSiF' 
    , 'data' => $data 
); 

print_r($params); 

//输出

Array 
(
    [Format] => json 
    [Target] => Offer 
    [Method] => create 
    [Service] => HasOffers 
    [Version] => 2 
    [NetworkId] => demo 
    [NetworkToken] => NETU2nzMw8AYS6EGgjFrjGR88GcSiF 
    [data] => Array 
     (
      [0] => Array 
       (
        [name] => LELE 
        [description] => test 
        [offer_url] => http://google.nl 
        [preview_url] => http://google.nl 
        [expiration_date] => 08-08-2013 
       ) 

      [1] => Array 
       (
        [name] => LOLO 
        [description] => test 
        [offer_url] => http://google.nl 
        [preview_url] => http://google.nl 
        [expiration_date] => 08-08-2013 
       ) 

     ) 

) 
+0

中指定像这样的多维数组你是否知道这与http://www.hasoffers.com/wiki/Offer:create一起工作或者它是一个通用的答案? – VolkerK 2013-02-28 09:11:46

0

a)如何保存一个数组中有多个数据集。

$fixed_params = array(
'Format' => 'json' 
,'Target' => 'Offer' 
,'Method' => 'create' 
,'Service' => 'HasOffers' 
,'Version' => 2 
,'NetworkId' => 'demo' 
,'NetworkToken' => '....' 
); 

$offer_data = array(
array(
     'name' => 'LOLO' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 
), 
array(
     'name' => 'LELE' 
     ,'description' => 'test' 
     ,'offer_url' => 'http://google.nl' 
     ,'preview_url' => 'http://google.nl' 
     ,'expiration_date' => '08-08-2013' 
) 
); 

// store all results here 
$result = array(); 

// iterates two times since $offer_data has two elements. 
foraech ($offer_data as $offern => $data){ 
// store offer's data into fixed_params['data'] element. 
$fixed_params['data'] = $data; 

$url = $base . http_build_query($fixed_params); 

$result[] = json_decode(file_get_contents($url)); 
} 

print_r($result); 

B)API是否接受它....

据我了解的API,它接受一个建立在时间。见http://www.hasoffers.com/wiki/Offer:create它说:创建一个新的报价。