Guest User

Untitled

a guest
Feb 25th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. <?php
  2. // Specify API URL
  3. define('HASOFFERS_API_URL', 'https://api.hasoffers.com/Apiv3/json');
  4.  
  5. // Specify method arguments
  6. $args = array(
  7. 'NetworkId' => 'xxxxx',
  8. 'Target' => 'Offer',
  9. 'Method' => 'findAll',
  10. 'NetworkToken' => 'xxxx',
  11. 'fields' => array(
  12. 'id',
  13. 'name'
  14. )
  15. );
  16.  
  17. // Initialize cURL
  18. $curlHandle = curl_init();
  19.  
  20. // Configure cURL request
  21. curl_setopt($curlHandle, CURLOPT_URL, HASOFFERS_API_URL . '?' . http_build_query($args));
  22.  
  23. // Make sure we can access the response when we execute the call
  24. curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
  25.  
  26. // Execute the API call
  27. $jsonEncodedApiResponse = curl_exec($curlHandle);
  28.  
  29. // Ensure HTTP call was successful
  30. if($jsonEncodedApiResponse === false) {
  31. throw new RuntimeException(
  32. 'API call failed with cURL error: ' . curl_error($curlHandle)
  33. );
  34. }
  35.  
  36. // Clean up the resource now that we're done with cURL
  37. curl_close($curlHandle);
  38.  
  39. // Decode the response from a JSON string to a PHP associative array
  40. $apiResponse = json_decode($jsonEncodedApiResponse, true);
  41.  
  42. // Make sure we got back a well-formed JSON string and that there were no
  43. // errors when decoding it
  44. $jsonErrorCode = json_last_error();
  45. if($jsonErrorCode !== JSON_ERROR_NONE) {
  46. throw new RuntimeException(
  47. 'API response not well-formed (json error code: ' . $jsonErrorCode . ')'
  48. );
  49. }
  50.  
  51. // Print out the response details
  52. if($apiResponse['response']['status'] === 1) {
  53. // No errors encountered
  54. //echo 'API call successful';
  55. echo PHP_EOL;
  56. echo print_r($apiResponse['response']['data'], true);
  57. echo PHP_EOL;
  58. }
  59. else {
  60. // An error occurred
  61. echo 'API call failed (' . $apiResponse['response']['errorMessage'] . ')';
  62. echo PHP_EOL;
  63. echo 'Errors: ' . print_r($apiResponse['response']['errors'], true);
  64. echo PHP_EOL;
  65. }
  66.  
  67. 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 ) )
  68.  
  69. foreach ($apiResponse['response']['data'] as $data) {
  70. echo $data['Offer']['id'] . ", " . $data['Offer']['name'];
  71. }
  72.  
  73. ini_set('display_errors', 1);
  74. error_reporting(E_ALL);
Add Comment
Please, Sign In to add comment