Advertisement
Alexxxxxxxxxxxxxx

Untitled

Jan 22nd, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Базовая конфигурация
  5. */
  6. // * Апи ключ вашего акканута
  7. $apiKey = 'f82c8ca5fa0390fbc8dcc2bac3fcc791';
  8. // * Домен проекта на который происходит отправка заказов
  9. $domain = 'shakes.pro';
  10. // Урл оригинального лендинга, необходим для корректого расчета Вашей статистики
  11. $landingUrl = 'http://ptb.amaroksale.com';
  12. // * Идентификатор оффера на который вы льете
  13. $offerId = '3697';
  14. // Код потока заведенного в системе, если указан, статистика будет записываться на данный поток
  15. $streamCode = 'ew1l';
  16. // Страница, отдаваемая при успешном заказе
  17. $successPage = 'success.php';
  18. // Страница, отдаваемая в случае ошибки
  19. $errorPage = 'index.html';
  20. /**
  21. * Формирование отправляемого заказа
  22. */
  23. $url = "http://$domain?r=/api/order/in&key=$apiKey";
  24. $order = [
  25. 'countryCode' => (!empty($_POST['country']) ? $_POST['country'] : ($_GET['country'] ? $_GET['country'] : 'RU')),
  26. 'createdAt' => date('Y-m-d H:i:s'),
  27. 'ip' => (!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null), // ip пользователя
  28. 'landingUrl' => $landingUrl,
  29. 'name' => (!empty($_POST['name']) ? $_POST['name'] : ($_GET['name'] ? $_GET['name'] : '')),
  30. 'offerId' => $offerId,
  31. 'phone' => (!empty($_POST['phone']) ? $_POST['phone'] : ($_GET['phone'] ? $_GET['phone'] : '')),
  32. 'referrer' => (!empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null),
  33. 'streamCode' => $streamCode,
  34. 'sub1' => (!empty($_POST['sub1']) ? $_POST['sub1'] : ''),
  35. 'sub2' => (!empty($_GET['sub2']) ? $_GET['sub2'] : ''),
  36. 'sub3' => (!empty($_GET['sub3']) ? $_GET['sub3'] : ''),
  37. 'sub4' => (!empty($_GET['sub4']) ? $_GET['sub4'] : ''),
  38. 'userAgent' => (!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '-'),
  39. ];
  40.  
  41. /**
  42. * Отправка заказа
  43. */
  44. /**
  45. * @see http://php.net/manual/ru/book.curl.php
  46. */
  47. $curl = curl_init();
  48. /**
  49. * @see http://php.net/manual/ru/function.curl-setopt.php
  50. */
  51. curl_setopt($curl, CURLOPT_URL, $url);
  52. curl_setopt($curl, CURLOPT_POST, true);
  53. curl_setopt($curl, CURLOPT_POSTFIELDS, $order);
  54. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  55. /**
  56. * @see http://php.net/manual/ru/language.exceptions.php
  57. */
  58. try {
  59. $responseBody = curl_exec($curl);
  60. // тело оказалось пустым
  61. if (empty($responseBody)) {
  62. throw new Exception('Error: Empty response for order. ' . var_export($order, true));
  63. }
  64. /**
  65. * @var StdClass $response
  66. */
  67. $response = json_decode($responseBody, true);
  68. // возможно пришел некорректный формат
  69. if (empty($response)) {
  70. throw new Exception('Error: Broken json format for order. ' . PHP_EOL . var_export($order, true));
  71. }
  72. // заказ не принят API
  73. if ($response['status'] != 'ok') {
  74. throw new Exception('Success: Order is accepted. '
  75. . PHP_EOL . 'Order: ' . var_export($order, true)
  76. . PHP_EOL . 'Response: ' . var_export($response, true)
  77. );
  78. }
  79. /**
  80. * логируем данные об обработке заказа
  81. * @see http://php.net/manual/ru/function.file-put-contents.php
  82. */
  83. @file_put_contents(
  84. __DIR__ . '/order.success.log',
  85. date('Y.m.d H:i:s') . ' ' . $responseBody,
  86. FILE_APPEND
  87. );
  88. curl_close($curl);
  89.  
  90. if(!empty($successPage) && is_file(__DIR__ . '/' . $successPage)) {
  91. include __DIR__ . '/' . $successPage;
  92. }
  93. } catch (Exception $e) {
  94. /**
  95. * логируем ошибку
  96. * @see http://php.net/manual/ru/function.file-put-contents.php
  97. */
  98. @file_put_contents(
  99. __DIR__ . '/order.error.log',
  100. date('Y.m.d H:i:s') . ' ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString(),
  101. FILE_APPEND
  102. );
  103.  
  104. if(!empty($errorPage) && is_file(__DIR__ . '/' . $errorPage)) {
  105. include __DIR__ . '/' . $errorPage;
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement