PHP多维数组x HasOffers

时间:2016-06-27 13:08:42

标签: php json for-loop curl multidimensional-array

我用阵列卡住了几天。我正在使用HasOffers平台,我不知道是否有人知道它,但问题就在于此。

我正在调用API调用以从数据库中导出一些数据。我现在导出ID和NAME,它们是在cURL / JSON函数中的数组中。我在print_r()函数中看到了多维数组。但是当我试图进入循环时,就是不行。

这是API调用的代码:

<?php
// Specify API URL
define('HASOFFERS_API_URL', 'https://api.hasoffers.com/Apiv3/json');

// Specify method arguments
$args = array(
    'NetworkId' => 'xxxxx',
    'Target' => 'Offer',
    'Method' => 'findAll',
    'NetworkToken' => 'xxxx',
    'fields' => array(
        'id',
        'name'
    )
);

// Initialize cURL
$curlHandle = curl_init();

// Configure cURL request
curl_setopt($curlHandle, CURLOPT_URL, HASOFFERS_API_URL . '?' . http_build_query($args));

// Make sure we can access the response when we execute the call
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);

// Execute the API call
$jsonEncodedApiResponse = curl_exec($curlHandle);

// Ensure HTTP call was successful
if($jsonEncodedApiResponse === false) {
    throw new \RuntimeException(
        'API call failed with cURL error: ' . curl_error($curlHandle)
    );
}

// Clean up the resource now that we're done with cURL
curl_close($curlHandle);

// Decode the response from a JSON string to a PHP associative array
$apiResponse = json_decode($jsonEncodedApiResponse, true);

// Make sure we got back a well-formed JSON string and that there were no
// errors when decoding it
$jsonErrorCode = json_last_error();
if($jsonErrorCode !== JSON_ERROR_NONE) {
    throw new \RuntimeException(
        'API response not well-formed (json error code: ' . $jsonErrorCode . ')'
    );
}

// Print out the response details
if($apiResponse['response']['status'] === 1) {
    // No errors encountered
    //echo 'API call successful';
    echo PHP_EOL;
    echo print_r($apiResponse['response']['data'], true);
    echo PHP_EOL;
}
else {
    // An error occurred
    echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')';
    echo PHP_EOL;
    echo 'Errors: ' . print_r($apiResponse['response']['errors'], true);
    echo PHP_EOL;
}

这是上述调用结果的一部分:

Array ( [521] => Array ( [Offer] => Array ( [id] => 521 [name] => 360 Imprimir - BR ) ) [663] => Array ( [Offer] => Array ( [id] => 663 [name] => 360 Imprimir - BR ) ) [627] => Array ( [Offer] => Array ( [id] => 627 [name] => 360 Imprimir Venda Cartões e Folder - BR ) ) [1048] => Array ( [Offer] => Array ( [id] => 1048 [name] => Aasha - IN ) ) [412] => Array ( [Offer] => Array ( [id] => 412 [name] => ABA - English ) ) [573] => Array ( [Offer] => Array ( [id] => 573 [name] => Aba English - BR ) ) [581] => Array ( [Offer] => Array ( [id] => 581 [name] => Aba English - IT ) ) [575] => Array ( [Offer] => Array ( [id] => 575 [name] => Aba English ES ) ) [579] => Array ( [Offer] => Array ( [id] => 579 [name] => Aba English FR ) ) [577] => Array ( [Offer] => Array ( [id] => 577 [name] => Aba English MX ) ) [224] => Array ( [Offer] => Array ( [id] => 224 [name] => Abrinq ) ) [513] => Array ( [Offer] => Array ( [id] => 513 [name] => Acelerador Digital - BR ) ) [218] => Array ( [Offer] => Array ( [id] => 218 [name] => ActionAid - Apadrinhe Uma Criança ) ) [168] => Array ( [Offer] => Array ( [id] => 168 [name] => ActionAid - Apadrinhe uma criança ) ) [496] => Array ( [Offer] => Array ( [id] => 496 [name] => ActionAid - Apadrinhe Uma Criança Display ) ) [718] => Array ( [Offer] => Array ( [id] => 718 [name] => Adrian - US ) ) [374] => Array ( [Offer] => Array ( [id] => 374 [name] => Agarre ) ) [442] => Array ( [Offer] => Array ( [id] => 442 [name] => Agarre Brasil ) ) [676] => Array ( [Offer] => Array ( [id] => 676 [name] => Aladim Shop - BR ) ) [1050] => Array ( [Offer] => Array ( [id] => 1050 [name] => Alisha - IN ) ) [1259] => Array ( [Offer] => Array ( [id] => 1259 [name] => Alternativa de inversion - ES ) ) [844] => Array ( [Offer] => Array ( [id] => 844 [name] => Amanco - BR ) ) [1313] => Array ( [Offer] => Array ( [id] => 1313 [name] => Apple - AU ) ) [1315] => Array ( [Offer] => Array ( [id] => 1315 [name] => Apple - FI ) ) [1317] => Array ( [Offer] => Array ( [id] => 1317 [name] => Apple - FR ) ) [104] => Array ( [Offer] => Array ( [id] => 104 [name] => Assinatura Premiada - Editora Abril ) ) [134] => Array ( [Offer] => Array ( [id] => 134 [name] => Assine Sky ) ) [1217] => Array ( [Offer] => Array ( [id] => 1217 [name] => Auto - CO ) ) [1215] => Array ( [Offer] => Array ( [id] => 1215 [name] => Auto - MX ) ) [651] => Array ( [Offer] => Array ( [id] => 651 [name] => Avon Revendedora Virtual - BR ) ) [852] => Array ( [Offer] => Array ( [id] => 852 [name] => Ações que pagam dividendos - BR ) ) [680] => Array ( [Offer] => Array ( [id] => 680 [name] => Babbel CPA - BR ) ) [678] => Array ( [Offer] => Array ( [id] => 678 [name] => Babbel CPL - BR ) ) [565] => Array ( [Offer] => Array ( [id] => 565 [name] => Be2 BR ) ) [448] => Array ( [Offer] => Array ( [id] => 448 [name] => Bebedouros ) ) [402] => Array ( [Offer] => Array ( [id] => 402 [name] => Bem Direto ) ) [80] => Array ( [Offer] => Array ( [id] => 80 [name] => Bem Mais Seguro ) ) [398] => Array ( [Offer] => Array ( [id] => 398 [name] => Bidu ) ) [148] => Array ( [Offer] => Array ( [id] => 148 [name] => Big Prêmio - CPC ) ) [418] => Array ( [Offer] => Array ( [id] => 418 [name] => Bio Ritmo - Free Pass (OLD) ) ) [613] => Array ( [Offer] => Array ( [id] => 613 [name] => Bio Ritmo Free Pass - BR ) ) [756] => Array ( [Offer] => Array ( [id] => 756 [name] => Bio Ritmo Summer Pass - BR ) ) [506] => Array ( [Offer] => Array ( [id] => 506 [name] => Bio Ritmo Winterpass ) ) [182] => Array ( [Offer] => Array ( [id] => 182 [name] => Bom Negocio ) ) [60] => Array ( [Offer] => Array ( [id] => 60 [name] => Bora Viagem ) ) [200] => Array ( [Offer] => Array ( [id] => 200 [name] => Brasil Bingo ) ) [56] => Array ( [Offer] => Array ( [id] => 56 [name] => Brastemp ) ) [826] => Array ( [Offer] => Array ( [id] => 826 [name] => Buscar Ead - BR ) ) [635] => Array ( [Offer] => Array ( [id] => 635 [name] => Candidatos ) ) [728] => Array ( [Offer] => Array ( [id] => 728 [name] => Capital Strategy - IT ) ) [284] => Array ( [Offer] => Array ( [id] => 284 [name] => Cappoia ) ) [1193] => Array ( [Offer] => Array ( [id] => 1193 [name] => Captain Vape ) ) [124] => Array ( [Offer] => Array ( [id] => 124 [name] => Carro dos Sonhos ) ) [569] => Array ( [Offer] => Array ( [id] => 569 [name] => Carsystem - BR ) ) [376] => Array ( [Offer] => Array ( [id] => 376 [name] => CarSystem - Parceiro ) )

有人可以帮帮我吗?我需要将这个多维数组放在一个循环中。放入选择或表格。

谢谢!

1 个答案:

答案 0 :(得分:0)

我假设您提供的代码的结果是$apiResponse['response']['data']的值。所以循环将是:

foreach ($apiResponse['response']['data'] as $data) {
   echo $data['Offer']['id'] . ", " . $data['Offer']['name'];
}

在非生产性服务器上调试此类错误的小建议 - 将error_reporting指令添加到脚本的开头(或在引导文件中):

ini_set('display_errors', 1);
error_reporting(E_ALL);

这样就可以立即看到任何不存在的错误,如访问不存在的数组键。