curl request through TLS v1.0 - php

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

Related

Curl error with client certificate authentication

I have to call an api that requires the client certificate authentication.
I test this api in postman with client certification and its working perfectly.
Now i want to do it with php curl.
I tried below code but its giving me following error.
HTTP/2 stream 0 was not closed cleanly: HTTP_1_1_REQUIRED (err 13)
$url = 'https://someurl';
$json = 'somejson';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($ch, CURLOPT_SSLCERT, __DIR__."/certificate.pem");
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, __DIR__."/privatekey.key");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
$responseData = curl_exec($ch);
if(curl_errno($ch)) {
echo curl_error($ch);
}
curl_close($ch);
The error message has all the clues you need: HTTP_1_1_REQUIRED
TLS client certificates are not supported for HTTP/2, so you need to make sure your curl request is done using HTTP/1.1 by explicitly asking for that. curl will by default upgrade to HTTP/2 on HTTPS URLs otherwise.
Set CURLOPT_HTTP_VERSION to CURL_HTTP_VERSION_1_1.

how to resolve ( [error] => invalid_client [error_description] => client authentication failed ) for ebay

public function authorizationToken(){
$link = "https://api.ebay.com/identity/v1/oauth2/token";
$codeAuth = base64_encode($this->clientID.':'.$this->certID);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Authorization: Basic '.$codeAuth));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=authorization_code&code=".urlencode($this->authCode)."&redirect_uri=".$this->ruName."&scope=".urlencode('https://api.ebay.com/oauth/api_scope'));
$response = curl_exec($ch);
$json = json_decode($response, true);$info = curl_getinfo($ch);
curl_close($ch);print_r($json);}
It looks like you are getting refresh_token confused with authorization_token, because you are mixing incompatible parameters between the two different requests.
If you are trying to get the initial "AccessToken" (AKA UserAccessToken, access_token, AuthToken, ...), then you should not include the scope parameter in your request body. So your request body should look like this:
grant_type=authorization_code&redirect_uri=<redirect_uri>&code=<authorization_code>
Where <authorization_code> is the value that is returned from here.
For an overview of the difference, I'd recommend this answer over the official docs on the topic.
Try this code
public function authorizationToken(){
$link = "https://api.ebay.com/identity/v1/oauth2/token";
$codeAuth = base64_encode($this->clientID.':'.$this->certID);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Authorization: Basic '.$codeAuth));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=".urlencode('https://api.ebay.com/oauth/api_scope'));
$response = curl_exec($ch);
$json = json_decode($response, true);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($json);
}

PUT request using cURL PHP returns error 415

I'm trying now to execute PUT request using cURL PHP.
Unfortunately, I receive an error "HTTP Status 415 - Unsupported Media Type".
Here is my code :
$ch = curl_init();
$proxy = 'api.test.sandbox.mobile.de';
$proxy_port = "8080";
$loginpassw = 'XXX:YYY';
$url='https://services.mobile.de/seller-api/sellers/1086/ads/509939';
$headers = array();
$headers[] = "Content-type: application/json";
$data = array(
'mileage' => '10000',
);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy_port);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $loginpassw);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
echo $data;
curl_close($ch);
What is wrong with py request ?
According to their API, you need to send data in either JSON or XML format and specify Content-Type header
https://services.mobile.de/docs/seller-api.html#_media_types
So set the header for writing:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/vnd.de.mobile.api+json');
And change POSTFIELDS to
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

Difficulty with getting TLS 1.2 to work with cURL

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.

How to use TLS protocol on cURL?

Actually the code:
$data = '{"params": ["testing_true", "bb4238e7-8f6d-4b2e-9ea4-43791bfb72df"], "jsonrpc": "2.0", "method": "gate.check", "id": 27034}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "tls://site.com");
curl_setopt($ch, CURLOPT_PORT, 4928);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data)
));
$output = curl_exec($ch);
var_dump(curl_error($ch));
var_dump(curl_errno($ch));
echo($output) . PHP_EOL;
curl_close($ch);
With this treatment I get
Protocol tls not supported or disabled in libcurl
Here is an example of recourse to TLS, as it is to make friends with cURL? How to make TLS connection from PHP in web server, and safely
You want HTTPS:// which is the name for the protocol that is HTTP over TLS.
So, "https://example.com" etc.

Categories