private function Cleantalk::sendRequest in Anti Spam by CleanTalk 7.2
Same name and namespace in other branches
- 7.5 src/Cleantalk.php \Cleantalk::sendRequest()
- 7 cleantalk.module \Cleantalk::sendRequest()
- 7.4 src/Cleantalk.php \Cleantalk::sendRequest()
Send JSON request to servers
Parameters
$msg:
Return value
boolean|\CleantalkResponse
1 call to Cleantalk::sendRequest()
- Cleantalk::httpRequest in ./
cleantalk.module - httpRequest
File
- ./
cleantalk.module, line 765 - Main CleanTalk integration module functions.
Class
- Cleantalk
- Cleantalk class create request
Code
private function sendRequest($data = null, $url, $server_timeout = 3) {
// Convert to array
$data = drupal_json_decode(drupal_json_encode($data));
// Convert to JSON
$data = drupal_json_encode($data);
if (isset($this->api_version)) {
$url = $url . $this->api_version;
}
// Switching to secure connection
if ($this->ssl_on && !preg_match("/^https:/", $url)) {
$url = preg_replace("/^(http)/i", "\$1s", $url);
}
$result = false;
$curl_error = null;
if (function_exists('curl_init')) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $server_timeout);
curl_setopt($ch, CURLOPT_POST, 1);
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:',
));
// see http://stackoverflow.com/a/23322368
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
// Disabling CA cert verivication
// Disabling common name verification
if ($this->ssl_on) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$result = curl_exec($ch);
if (!$result) {
$curl_error = curl_error($ch);
}
curl_close($ch);
}
if (!$result) {
$allow_url_fopen = ini_get('allow_url_fopen');
if (function_exists('file_get_contents') && isset($allow_url_fopen) && $allow_url_fopen == '1') {
$opts = array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: text/html\r\n",
'content' => $data,
'timeout' => $server_timeout,
),
);
$context = stream_context_create($opts);
$result = @file_get_contents($url, false, $context);
}
}
if (!$result) {
$response = null;
$response['errno'] = 1;
if ($curl_error) {
$response['errstr'] = sprintf("CURL error: '%s'", $curl_error);
}
else {
$response['errstr'] = 'No CURL support compiled in';
}
$response['errstr'] .= ' or disabled allow_url_fopen in php.ini.';
$response = (object) drupal_json_decode(drupal_json_encode($response));
return $response;
}
$errstr = null;
$response = (object) drupal_json_decode($result);
if ($result !== false && is_object($response)) {
$response->errno = 0;
$response->errstr = $errstr;
}
else {
$errstr = 'Unknown response from ' . $url . '.' . ' ' . $result;
$response = null;
$response['errno'] = 1;
$response['errstr'] = $errstr;
$response = (object) drupal_json_decode(drupal_json_encode($response));
}
return $response;
}