【发布时间】:2013-02-28 09:03:37
【问题描述】:
下面的脚本显然使用了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
)
)
【问题讨论】:
-
你有一个数组,里面只有一个元素,里面的键值重复,所以最后一个被记住了。
-
但是当我运行脚本时,它会显示 [0]=>... 和 [1]=>... 我如何在不获取此值的情况下执行此操作,因为只有 Hasoffer 平台的 API接受 'data'=>[name].. not 'data'=>[0]=>[name]
-
答案可能是(并且浏览了我认为是的 api 文档):您必须发送两个单独的请求,然后创建两个单独的报价。
-
如果为这两个数组设置名称('data' => array('first'=> array('name' =>'LOLO','description'=>'test ' ,'offer_url' => 'google.nl' ,'preview_url' => 'google.nl' ,'expiration_date' => '08-08-2013'), 'second' => array('name' => 'LELE' ,'description' => 'test' ,'offer_url' => 'google.nl' ,'preview_url' => 'google.nl' ,'expiration_date' => '08-08-2013' ))) ?
-
不,它不起作用.. 是否可以在每个数组的某种循环中设置请求?这样它每次都以新值循环