下面的脚本显然使用了http://www.hasoffers.com/wiki/Offer:create中记录的API。该问题至少有两部分:a)如何在数组中存储多个数据集。 b)API是否接受它......
当我运行脚本时,它只将最后一个值存储在'data'中,如何让它一次存储更多数据?
以下代码包含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 :(得分: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'))
答案 1 :(得分: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(
答案 2 :(得分: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
)
)
)
答案 3 :(得分: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它说:创建新优惠。
答案 4 :(得分: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连接一次,并且只允许一个值。