Advertisement
Trigub_Ilia

POST запрос с параметрами

Dec 25th, 2018
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?>
  2. <?
  3. // Ваш ключ доступа к API (из Личного Кабинета)
  4. $api_key = "";
  5.  
  6. // Данные о новом контакте
  7. $user_email = $_POST["email"];
  8. //$user_name = iconv('cp1251', 'utf-8', "Василий Иванович Чапаев");
  9. $user_lists = "16357969";
  10. $user_tag = urlencode("Site subscriber");
  11.  
  12. // Создаём POST-запрос
  13. $POST = array (
  14.   'api_key' => $api_key,
  15.   'list_ids' => $user_lists,
  16.   'fields[email]' => $user_email,
  17.     //'fields[Name]' => $user_name,
  18.   'tags' => $user_tag
  19. );
  20.  
  21. // Устанавливаем соединение
  22. $ch = curl_init();
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24. curl_setopt($ch, CURLOPT_POST, 1);
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
  26. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  27. curl_setopt($ch, CURLOPT_URL, 'https://api.unisender.com/ru/api/subscribe?format=json');
  28. $result = curl_exec($ch);
  29. if ($result) {
  30.   // Раскодируем ответ API-сервера
  31.   $jsonObj = json_decode($result);
  32.   if(null===$jsonObj) {
  33.     // Ошибка в полученном ответе
  34.     echo "Invalid JSON";
  35.   }
  36.   elseif(!empty($jsonObj->error)) {
  37.     // Ошибка добавления пользователя
  38.     echo "An error occured: " . $jsonObj->error . "(code: " . $jsonObj->code . ")";
  39.   } else {
  40.     // Новый пользователь успешно добавлен
  41.     echo "Added. ID is " . $jsonObj->result->person_id;
  42.   }
  43. } else {
  44.   // Ошибка соединения с API-сервером
  45.   echo "API access error";
  46. }
  47. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement