private function Cleantalk::sendRequest in Anti Spam by CleanTalk 8.4
Same name and namespace in other branches
- 9.1.x src/lib/Cleantalk/Antispam/Cleantalk.php \Cleantalk\Antispam\Cleantalk::sendRequest()
Send JSON request to servers
Parameters
$msg:
Return value
boolean|\CleantalkResponse
1 call to Cleantalk::sendRequest()
- Cleantalk::httpRequest in src/
lib/ Cleantalk/ Antispam/ Cleantalk.php - httpRequest
File
- src/
lib/ Cleantalk/ Antispam/ Cleantalk.php, line 272
Class
- Cleantalk
- Cleantalk class create request
Namespace
Cleantalk\AntispamCode
private function sendRequest($data, $url, $server_timeout = 15) {
// Convert to array
$data = (array) json_decode(json_encode($data), true);
$original_url = $url;
$original_data = $data;
//Cleaning from 'null' values
$tmp_data = array();
foreach ($data as $key => $value) {
if ($value !== null) {
$tmp_data[$key] = $value;
}
}
$data = $tmp_data;
unset($key, $value, $tmp_data);
// Convert to JSON
$data = 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);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Disabling CA cert verivication and
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
// Disabling common name verification
if ($this->ssl_on && $this->ssl_path != '') {
curl_setopt($ch, CURLOPT_CAINFO, $this->ssl_path);
}
$result = curl_exec($ch);
if (!$result) {
$curl_error = curl_error($ch);
// Use SSL next time, if error occurs.
if (!$this->ssl_on) {
$this->ssl_on = true;
return $this
->sendRequest($original_data, $original_url, $server_timeout);
}
}
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 || !cleantalk_is_JSON($result)) {
$response = null;
$response['errno'] = 1;
$response['errstr'] = true;
$response['curl_err'] = isset($curl_error) ? $curl_error : false;
$response = json_decode(json_encode($response));
return $response;
}
$errstr = null;
$response = 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 = json_decode(json_encode($response));
}
return $response;
}