function sendRawRequest in Anti Spam by CleanTalk 8
Same name and namespace in other branches
- 7 cleantalk.module \sendRawRequest()
- 7.2 cleantalk.module \sendRawRequest()
Function sends raw request to API server
Parameters
string url of API server:
array data to send:
boolean is data have to be JSON encoded or not:
integer connect timeout:
Return value
type
4 calls to sendRawRequest()
- BootSubscriber::onEvent in src/
EventSubscriber/ BootSubscriber.php - cleantalk_boot in ./
cleantalk.module - Implements hook_boot()
- getAutoKey in ./
cleantalk.module - Function gets access key automatically
- noticePaidTill in ./
cleantalk.module - Function gets information about renew notice
File
- ./
cleantalk.module, line 63 - Main CleanTalk integration module functions.
Code
function sendRawRequest($url, $data, $isJSON = false, $timeout = 3) {
$result = null;
if (!$isJSON) {
$data = http_build_query($data);
}
else {
$data = \Drupal\Component\Serialization\Json::encode($data);
}
$curl_exec = false;
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// resolve 'Expect: 100-continue' issue
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Expect:',
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = @curl_exec($ch);
if ($result !== false) {
$curl_exec = true;
}
@curl_close($ch);
}
if (!$curl_exec) {
$opts = array(
'http' => array(
'method' => "POST",
'content' => $data,
),
);
$context = stream_context_create($opts);
$result = @file_get_contents($url, 0, $context);
}
return $result;
}