0

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


当我运行脚本时,它只将最后一个值存储在“数据”中,我怎样才能让它一次存储更多数据?

下面的代码具有例如 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
            )

    )
4

5 回答 5

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'))
于 2013-02-28T09:06:11.057 回答
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 (....)在您的array

array( array(

于 2013-02-28T09:07:35.257 回答
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
                )

        )

)
于 2013-02-28T09:10:07.113 回答
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它说:创建一个新的报价。

于 2013-03-01T10:52:28.793 回答
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 连接一次,并且只允许一个值。

于 2013-03-05T09:03:46.590 回答