<?php
/*
* Settings
*/
$formaUrl = 'https://1fhost.ru';
$formaUser = 'user';
$formaPwd = 'password';
/*
* Functions
*/
function setCurlCookies(&$ch, $cookies) {
foreach($cookies as $ckey => $cvalue) {
curl_setopt($ch, CURLOPT_COOKIE, $ckey . '=' . $cvalue);
}
}
/*
* Get auth cookies
*/
$authUrl = $formaUrl . "/iOSClientServices/Auth.ashx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "UserName=" . $formaUser . "&Pass=" . $formaPwd . "&IsDebug=1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = [];
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
if (curl_errno($ch)) {
die('Error:' . curl_error($ch));
}
curl_close ($ch);
/*
* Example request
*/
$url = $formaUrl . "/app/v1.0/api/info";
$ch = curl_init();
setCurlCookies($ch, $cookies);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_VERBOSE, true);
if (curl_errno($ch)) {
die('Error:' . curl_error($ch));
}
$output = curl_exec($ch);
curl_close($ch);
echo $output;
|