2013-02-28 24 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它說:創建一個新的報價。