Avatar of Teera Char
Teera Char
Flag for Thailand asked on

covert curl to php but "faultstring":"Content-Length is missing","detail

<?php
    $certFile = "/etc/apache2/ssl/thekohsamui/thekohsamui_com.crt";
    $keyFile = "/etc/apache2/ssl/thekohsamui/thaisanakoh.com.key" ;
    $headers = array(
        "Content-type: application/x-www-form-urlencoded",
        "Authorization: Basic RwerWkZWcGpJQ0E5Y2xScUFQZVowODhSQTVYTFgzNzk6QTNLNkxjVHU3OTZ1QTZteAxy"
        );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_URL,'https://openapi-test.thaisana.com/v1/qrpayment/request');
    curl_setopt($curl, CURLOPT_SSLCERT, $certFile);
    curl_setopt($curl, CURLOPT_SSLKEY, $keyFile);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  

    $result = curl_exec($curl);
    echo $result ;

    if (curl_errno($curl)) {
        echo 'Error:' . curl_error($curl);
    }
    curl_close($curl);
  ?>

Open in new window

PHP* Curl

Avatar of undefined
Last Comment
Teera Char

8/22/2022 - Mon
Teera Char

ASKER
This is my curl command


curl --cert /etc/apache2/ssl/thekohsamui/thekohsamui_com.crt \
 --key /etc/apache2/ssl/thekohsamui/thaisanakoh.com.key \
--location --request POST 'https://openapi-test.thaisana.com/v2/oauth/token' \
 --header 'Content-Type: application/x-www-form-urlencoded' \
 --header 'Authorization: Basic RwerWkZWcGpJQ0E5Y2xScUFQZVowODhSQTVYTFgzNzk6QTNLNkxjVHU3OTZ1QTZteAxy' \
 --data-urlencode 'grant_type=client_credentials'

This is result

{"token_type":"Bearer","client_id":"xyzZFVpjICA9clRqAPeZ088RwRTLX379","access_token":"xyzrhBVp4E54gASCo0DzpvW0sUR8","scop e":"","expires_in":"1799","status":"approved"}  




gr8gonzo

You're not sending any data in your POST, so it's not sending a content length header.

Try adding POST data like this:

curl_setopt($ch, CURLOPT_POSTFIELDS, $your_data);

Or if you don't actually have any data, then send along a Content-Length: 0 header.
Teera Char

ASKER
I Try

curl_setopt($curl, CURLOPT_POSTFIELDS, $headers);

Open in new window

error
{"fault":{"faultstring":"Content-Length is missing","detail":{"errorcode":"messaging.adaptors.http.flow.LengthRequired"}}}1

become
{ "code": "openapi_error", "message": "Invalid access token" } 1
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
David Favor

As @gr8gonzo mentioned, adding headers means Content-Length is still 0, so won't be set in headers.

You'll follow @gr8gonzo's comment by adding...

curl_setopt($ch, CURLOPT_POSTFIELDS, $your_data);

Open in new window

ASKER CERTIFIED SOLUTION
gr8gonzo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Teera Char

ASKER
Thank you very much