我已經修改這個代碼從一個JSON文件,而不是XML文件(我的舊代碼)PHP數組輸出中「空」的值(值從JSON文件中提取)
<?php
$tracking_id = 'MyID'; //This is used to track the user doing the offer. can be email, clickid, subid.. etc
$userip = $_SERVER['REMOTE_ADDR']; //We need to get the users ip, so the rss feed can display the correct offers for their country.
$user_agent = $_SERVER['HTTP_USER_AGENT']; //lets collect their user agent to pass along.
$max_offers = 5; //max number of offers to display.
$str = file_get_contents('https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213&geoip='.$userip.'&ua='.urlencode($user_agent).'&subid='.urlencode($tracking_id));
$json = json_decode($str, true);
$opOffer = array();
$offer_cnt = 0;
foreach($json['offers'] as $offeritem)
{
$opOffer[$offer_cnt] = array("title" => array($offeritem->title), "link" => array($offeritem->link));
$offer_cnt++;
};
if (0 == count($opOffer)):
echo 'Sorry there are no offers available for your region at this time.';
else:
echo json_encode($opOffer);
endif;
?>
獲取數據,它就像輸出這樣的:
[{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]},{"title":[null],"link":[null]}]
它應該是這樣的:
[{"title":[{"What is Your Favorite Time for McDonald's?"}],"link":[{"https:\/\/filetrkr.com\/show.php?l=0&u=31802&id=5882&tracking_id=MyId"}]}]
鏈接看到JSON輸出:https://www.cpalead.com/dashboard/reports/campaign_json_load_offers.php?id=296213
現在的輸出的確切格式使用該: $ opOffer [$ offer_cnt] =陣列( 「標題」=> $ offeritem [ '標題'], 「鏈接」=> $ offeritem ['連結「]); 只需要5個優惠($ max_offers = 5;)並非所有優惠 – HASH717
因此,在foreach中,測試max記錄並跳出循環:if(cnt> 5){break; } –
是它的工作,但輸出showup這樣的: [{},{},{}] 它應該是這樣的: [{[{}],[{}],[{}]} ] – HASH717