Difficulty with getting TLS 1.2 to work with cURL - php

I am having difficulty curling an HTTPS url that uses TLS 1.2, in my curl operation- I post my login data into the website and save it in cookiefile. The error message I am getting is this:
"API Error:SSL connect error"
I have tried setting curl_setopt($ch, CURLOPT_POST) to 6 but that does not seem to work, any suggestions?
Versions I am using:
OpenSSL version is 1.0.2b
CURL version is 7.49.1
PHP is 5.5
Here is the code:
public function curl_request($url, $data, $method = 'GET', $headers = array(), $ssl_verify = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $ssl_verify);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $ssl_verify);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_HTTP200ALIASES, range(400, 599));
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
if ($method == 'POST')
{
curl_setopt($ch, CURLOPT_POST, 6);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$resp = curl_exec($ch);
if(curl_errno($ch))
{
throw new Exception('Unleashed API Error:' . curl_error($ch));
}
//print("<pre>" . print_r($resp, true). "</pre>")
$info = curl_getinfo($ch);
curl_close($ch);
$output = array();
$output['resp'] = $resp;
$output['info'] = $info;

OpenSSL version is 0.9.8b
There is no support for TLS 1.2 in OpenSSL 0.9.8. You need at least OpenSSL version 1.0.1.

Related

Connecting to VCenter with PHP using REST API authentication error

I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

curl request through TLS v1.0

Using below code to send request over TLS v1.0.
$url = "https://example.com";
$input_segment = "";
$data = array('InputSegments'=>$input_segment, 'cmdSubmit'=>'Submit');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_CIPHER_LIST, 'TLSv1');
curl_setopt($ch, CURLOPT_PORT, "443");
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded'
));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$err_no = curl_errno($ch);
$err = curl_error($ch);
curl_close($ch);
var_dump($response);
ERROR
Error #:SSL read: error:00000000:lib(0):func(0):reason(0), errno 104
It looks like still using SSL. How to send curl request over TLSv1.0 in PHP?
to force TLS 1.0, i think it's sufficient to set CURLOPT_SSLVERSION to CURL_SSLVERSION_TLSv1_0

SSL connect error error 35 in paypal curl request

When i am making a payment in paypal, its throws "SSL connect error (35)", even i have a enabled TLS. And also i have tested with "https://tlstest.paypal.com", is also return same error.
Following sample i used to test TLS.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://tlstest.paypal.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2
curl_setopt($ch, CURLOPT_CAINFO, 'cacert.pem');
$result = curl_exec($ch);
echo 'result = '.$result.'<br>';
echo 'errno = '.curl_errno($ch).'<br>';
echo 'error = '.curl_error($ch).'<br>';
curl_close($ch);
Thanks in advance

Curl:Unknown SSL protocol error gcm

enter image description hereI have designed gcm server in php(MAMP). curl version-7.43.0,php version 7.0.0 .The code for server-
<?php
function send_push_notification($registration_id,$message){
define('API_KEY','AIzaSyAoFih8qEFlOis3vVWhsxxlLxxxxxxxxxx');
$url='https://gcm-http.googleapis.com/gcm/send';
$fields=array(
'registration_id'=>$registration_id,
'data'=>$message,
);
$headers=array(
'Authorization:key='.API_KEY,
'Content-Type:application/json'
);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
// Close connection
curl_close($ch);
echo $result;
}
?>
<?php
send_push_notification("e2DgrsZPGKU:APA91bF_73a6-o5CLV-gdcZzYFAbtikJqi-5w6gDSCaRa4z8-1iGLeV5SS6hQkW8pj_g_DxBe7JLDsOPMGu3y1GkKw1vpn_ZEWIeCwbSITpd0pLwaz50W8uzHKNghvnf1xxxxxxxxxxx","hi");
?>
When I try running this php script in my browser it shows following error
Curl failed: Unknown SSL protocol error in connection to
gcm-http.googleapis.com:443
I have disabled SSL verification,still it shows curl error.
Changing SSL version to 6 worked for me.
curl_setopt($ch,CURLOPT_SSLVERSION,6);

Curl_exec return null in php

I have a problem to get the data using curl operation. Here i hide the token, If i use the url only in my browser then it returns the data but here its null.
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords?authtoken=".$token."&scope=crmapi";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
Where i do mistake please help me.
This is how you can try with CURLOPT_RETURNTRANSFER which is used to return the output and curl_errno() to track the errors :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/my_url.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
Helpful Links: curl_errno(), curl_error()
Try adding these lines:
<?php
$token = "TOKEN"; //the actual token hidden
$url = "https://crm.zoho.com/crm/private/xml/Leads/getRecords";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("authtoken"=>$token,"scope"=>"crmapi"));
$result = curl_exec($ch);
curl_close($ch);
echo $result; //does not return anything
?>
please see below article , not just turn off verify , you should update your php.ini with pem file
https://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/
Anyone running a recent Linux distribution is probably already OK, because they get the curl libraries bundled in through their package managers and recent curl libraries come with the latest CA root certificate bundle from Mozilla.org.
For a PHP installation that doesn’t come with this file, like the Windows PHP distribution, you need to download the CA root certificate bundle and tell PHP where to find it.

Categories