Error - PHP - cURL - Unknown SSL protocol error in connection - - php

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $xml_url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_HEADER, false);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
$xml = curl_exec($ch);
if(curl_exec($ch) === false)
{ echo curl_error($ch); }
else
{ echo 'Operation completed without any errors'; }
curl_close($ch);
return $xml;
Above code is giving below error.
Unknown SSL protocol error in connection to api.site.com:443
As per suggested by many people that below code will resolve above issue but it is not helping. Still getting same error.
curl_setopt ($ch, CURLOPT_SSLVERSION, 3);
I tried below also as per suggestions but still getting same error.
curl_setopt ($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3' );
Please suggest what else I should put in code to fix this error.
Thank you,

You just ignored the SSL certificate verification, may be it will help you to resolve the problem, use the code below when requesting with CURL.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

You could check which TLS version the website uses with curl verbose mode:
curl_setopt($curl, CURLOPT_VERBOSE, true);
Where the output will be like this:
...
SSL connection using TLS1.0 / RSA_3DES_EDE_CBC_SHA1
...
Then set CURLOPT_SSL_CIPHER_LIST like this:
curl_setopt($curl, CURLOPT_SSL_CIPHER_LIST, "TLSv1");
Where "TLSv1" is the TLS version the website uses.
Changing SSL version and VERIFY_PEER/HOST in curl options didn't solve my problem, but this approach did.

You may try this code.
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $xml_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_HEADER, false);
$xml = curl_exec($ch);
if($xml === false) {
echo curl_error($ch);
}else{
echo 'Operation completed without any errors';
}
curl_close($ch);
return $xml;
Also you may use this class for curl request,
https://gist.github.com/sourovroy/1fba93188470bbe3eefa15cd17533b23

Related

PHP - CURL error when trying to connect with proxy

I am trying to connect a website with CURL using a proxy in PHP. But I get "Received HTTP code 400 from proxy after CONNECT" error.
This is the code that I use to connect:
<?PHP
$ch = curl_init("https://ipinfo.io/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
curl_setopt($ch, CURLOPT_PROXY, "207.154.231.213:8080");
$output = curl_exec($ch);
if(curl_errno($ch))
echo curl_error($ch);
echo $output;
?>
I don't know what the problem is. The proxy should work perfectly because I check it.
use nex code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://ipinfo.io/json");
$result=curl_exec($ch);
curl_close($ch);
echo json_decode($result, true);
I recommend http://docs.guzzlephp.org/ library for apis for better practices
Based on the documentation curl_setopt you should either remove your CURLOPT_PROXYTYPE setting (to use the default CURLPROXY_HTTP), or you need to change your invalid setting
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
to
curl_setopt($ch, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
Note that the type HTTP does not exist.

PHP cURL cannot connect to IIS on local machine

I have a PHP script that tries to connect with an other PHP script located on a IIS installation running on a local machine in my office using the cURL protocol but fails to do so.
Local configuration:
I've opened the port 8789 on the firewall, I made the router redirect correctly to the IP of the machine running IIS and I've set the default port of IIS to 8789.
If I try to connect to the script (93.144.xxx.xxx:8987/curl_response.php) through a browser I get the resulting output so this setup is working correctly.
When I try to run the PHP script on a remote webserver though it gives my either cUrl error (#7): couldn't connect to host or cUrl error (#28): connect() timed out! (I'm trying different webservers)
The curl part of my PHP script is the following:
$url = 'http://93.144.xxx.xxx:8789/curl_response.php';
$verbose = fopen('php://temp', 'w+');
$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_STDERR, $verbose);
curl_setopt ($curl, CURLOPT_URL, $rurl);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 8789);
$response = curl_exec($curl);
if ($response === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($curl),
htmlspecialchars(curl_error($curl)));
rewind($verbose);
$verboseLog = stream_get_contents($verbose);
echo "Verbose information:\n<pre>", htmlspecialchars($verboseLog),"</pre>\n";
}else{
curl_close($curl);
// trim response (serialized array)
$response = trim($response);
// unserialize
$response = unserialize($response);
print_r($response);
}
I've tried several different CURLOPT_ configurations and I've even tried using a dyndns host instead of the IP, but to no avail.
Is there a IIS configuration to set so it will accept incomming cURL requests or is there a specific header to be sent though cURL?
Thanks.
You have a typo:
curl_setopt ($curl, CURLOPT_URL, $rurl);
Should be:
curl_setopt ($curl, CURLOPT_URL, $url);
Here's an example of a working curl request:
<?php
$url = 'http://thepointless.com:80/';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_VERBOSE, true);
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($curl, CURLOPT_HEADER, 0);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, false);
curl_setopt ($curl, CURLOPT_PORT , 80);
$response = curl_exec($curl);
print $response;
?>
It's just like yours, except three things:
It's connecting to port 80
It's not setting STDERR
The typo is fixed
Run cURL from the command line with --verbose to see more detailed information.
And check if ISS it's with cURL enabled:
extension=php_curl.dll

PHPcURL Error (60): SSL certificate issues: unable to get local issuer certificate

I am doing an api call which is seemingly throwing some errors. So basicall my script is doing a SOAP API post call using cURL and it throws an error that (60): SSL certificate problem: unable to get local issuer certificate
I have tried
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
but its not working.
I have also tried the following:
$ch = curl_init(); // initialize curl handle
//curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, "GeoTrustGlobalCA.crt");
curl_setopt($ch, CURLOPT_URL, $ENDPOINT);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
//curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
//curl_setopt($ch, CURLOPT_PORT, 80);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
if ($curl_errno > 0) {
echo "cURL Error ($curl_errno): $curl_error\n";
} else {
echo "Data received. To complete this transaction, enter your Bonga PIN on your handset. if you don't have one dial *126*5# for instructions\n";
echo $data;
}
curl_close($ch);
Nothing seems to be working. Any workarounds? Btw the $ENDPOINT is https://safaricom.co.ke
The site has several errors, like mismatch of the name (should be www.safaricom.co.ke
not safaricom.co.ke) and also an incomplete certificate chain and additionally a very insecure setup. No wonder the validation fails. Some browsers work when the correct name was used because they work around missing chain certificates. Other clients (like curl) and most mobile browsers will not work because they expect the site to be properly set up.
For detailed information see the SSLabs report.

SSL Certificate with PHP CURL

I'm using curl to send an xml file over https to the rightmove API - they supplied me with all the certificates.
I am getting the error :
60SSL certificate problem: unable to get local issuer
certificateResult =
I have tried everything i have found on every other stackoverflow post similar and nothing is working, i have tried downloading the old cacert.pem and changed the files in php.ini - ive installed my personal information file andcreated a certificate on the browser and local machine and nothing is removing the error 60.
This is my PHP :
<?php
$xml = file_get_contents("myxml.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__).'\mypem.pem');
curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE , 1);
$ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
echo "Result = ".$ch_result;
curl_close($ch);
?>
this has had me banging my head for days, i would be very grateful for any assistance.
For my particular case i needed to add the keyfile, sslcert and cert password.
//$xml = file_get_contents("thexmlfile.xml");
$xml= $propertyXml->asXML();
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\pemfile.pem');
curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, getcwd() . '\myjks.jks');
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '\pemfile.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "thesslpassword");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE , 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
echo "Result = ".$ch_result;
curl_close($ch);
It is failing as curl is unable to verify the certificate provided by the server.
There are two options to get this to work:
1 Allows curl to make insecure connections, that is curl does not verify the certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
2 Add the root CA (the CA signing the server certificate) in php.ini
curl.cainfo=/path/to/cacert.pem
You should use option 2 as thats the option that ensures that you are connecting to secure ftp server.
I was getting the same "SSL certificate problem: unable to get local issuer certificateResult" error with HTTP GET to a remote https site. I only needed to provide the CA cert to php curl, ie CURLOPT_CAINFO:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA_Bundle.crt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
print "curl_error: $error_msg <br>";
} else {
print "output: $output<br>";
}
curl_close($ch);

Logging into a PHP forum using lib-cURL and getting a page contents

This is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec ($ch);
echo $page;
curl_close($ch);
But I don't think it's successfully logging in as the site (my own, by the way) doesn't show a log of me signing in. I know the username and password are correct, as are the URLs. I get a cookie.txt file back with what looks like good data but I'm not sure.
If I try some basic debugging, like this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/login.php");
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=deleted&password=deleted');
curl_setopt ($ch, CURLOPT_COOKIEJAR, 'C:\xampp\htdocs\scrape\cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!$store = curl_exec($ch))
{
echo "login fail";
}
curl_setopt($ch, CURLOPT_URL, "http://www.membersite.com/memberlist.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(!$page = curl_exec($ch))
{
echo "page fail";
}
echo $page;
curl_close($ch);
I get a "page fail" being printed to the page, so I guess the logging in isn't working.
Any help? thanks.
Nevermind, downloaded a phpBB cURL library and sorted it.
Add the CURLOPT_COOKIEFILE option with same value as the CURLOPT_COOKIEJAR option.
You also need to use those two options for each subsequent request (ie your request for memberlist.php).
You can see a way of doing it here.

Categories