Skip to content

Instantly share code, notes, and snippets.

@flayder
Created September 28, 2016 07:29
Show Gist options
  • Save flayder/370d106c9561a249970128b8c8cac88b to your computer and use it in GitHub Desktop.
Save flayder/370d106c9561a249970128b8c8cac88b to your computer and use it in GitHub Desktop.
<?
if ($_SERVER['REQUEST_METHOD'] == 'POST'):
$result = array();
function fetchUrl($url, $postFields = array())
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
$headers = [
'Content-Type: application/json; charset=utf-8'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if(!empty($postFields)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = array();
$response['html'] = curl_exec($ch);
$response['err'] = curl_errno($ch);
$response['errmsg'] = curl_error($ch);
$response['header'] = curl_getinfo($ch);
curl_close($ch);
return $response;
}
$json = json_decode($_POST['json']);
$response = fetchUrl('http://rd.dev.rosdengi.com/api/leads?app_id=57a0a0869ab5746393c8c258&token=f7690ea0-82f3-11e6-8e33-b780694548e0&v=13', json_encode($json));
$result['result'] = $response;
$resp = json_decode($response['html']);
$error = '';
foreach ($resp as $keyses => $valueses)
{
if ($keyses == 'leadId') $error = 'N';
break;
}
if ($error === 'N')
{
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=\"utf-8\"";
$str = '';
foreach ($json as $k => $v)
{
if ($v == '' || $k == 'contacts') {
continue;
}
$str .= $k . ': ' . $v . "\r\n";
}
foreach ($resp as $ke => $va)
{
$leadId = $va;
break;
}
$message = "Данные пользователя: \r\n" . $str . "\r\n";
$message .= 'leadId: ' . $leadId . "\r\n";
/* и теперь отправим из */
mail('[email protected]', 'Json ответ с сервера API', $message, $headers);
}
echo json_encode($result);
die();
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment