简体   繁体   中英

Unable to post data to the remote server due to incompatible OpenSSL versions between remote server and local work-station

I am in a situation that I need to find another solution to post the data via PHP curl to the remote server without updating the current PHP version 5.4.9 to 7 or higher just to solve the OpenSSL incompatibilities.

Local Server

  • Operation System: Windows 7, 32 bit architecture
  • PHP version: 5.4.9
  • OpenSSL Version: 0.9.8x

Remote Server(https)

  • PHP version: 7.2.27
  • OpenSSL version: 1.0.2k

Here's my PHP code

<?php

$url = "https://****/api/daily/sales";
$headers = array(
    'AUTH-TOKEN: *****',
    'CONTENT-TYPE: application/json'
);

$items = array(
    'customer_no'=>'001',
    'customer_name'=>'John Smith',
    'customer_address'=>'Bohol Philippines',
    'customer_province'=>'Bohol',
    'customer_city'=>'Tagbilaran',
    'salesman_no'=>'001',
    'salesman_name'=>'Salesman A',
    'salesman_mobile_no'=>'0912345678',
    'supervisor_id'=>'001',
    'supervisor_name'=>'Supervisor A',
    'item_no'=>'001',
    'item_description'=>'Detergent',
    'qty'=>2,
    'price'=>5,
    'uom'=>'pcs',
    'sales_value'=>'10',
    'currency'=>'PHP'
);
    $data = array(
        'invoice_date'=>'2020-05-01',
        'items'=>array($items),
    );

echo post($data, $url, $headers);


function post($data,$url,$headers)
    {
        $payload = json_encode($data);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $curl_errno = curl_errno($ch);
        curl_close($ch);
        return $result;
    }

?>

C:\Users\ronvi\Projects\PHP> php index.php

The error returned upon posting via PHP curl

* About to connect() to (my website) port 443 (#0)
*   Trying (my web server IP address)...
* connected
* Connected to (my domain name) (my web server IP address) port 443 (#0)
* error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version
* Closing connection #0

Your suggestions or ideas are really appreciated thank you.

Thank you,

Ron

I solved it by using curl for windows as the data sender with https/http instead.

I downloaded the binary from the official website https://curl.haxx.se/windows/ , extracted it somewhere in the local machine and add the binary directory to the Environment variable, and updated the PHP code, see below.

<?php

$items = array(
    'customer_no'=>'001',
    'customer_name'=>'John Smith',
    'customer_address'=>'Bohol Philippines',
    'customer_province'=>'Bohol',
    'customer_city'=>'Tagbilaran',
    'salesman_no'=>'001',
    'salesman_name'=>'Salesman A',
    'salesman_mobile_no'=>'0912345678',
    'supervisor_id'=>'001',
    'supervisor_name'=>'Supervisor A',
    'item_no'=>'001',
    'item_description'=>'Detergent',
    'qty'=>2,
    'price'=>5,
    'uom'=>'pcs',
    'sales_value'=>'10',
    'currency'=>'PHP'
);
    $data = array(
        'invoice_date'=>'2020-05-01',
        'items'=>array($items),
    );

$json = json_encode($data);
$json = str_replace('"', '\"', $json); // using ' to wrap json data is not working in windows
$curl = "curl -X POST -H \"Content-Type: application/json\" -H \"AUTH-TOKEN: ******\" https://****.com.ph/api/daily/sales --data \"".$json."\" "; // CURL command
exec($curl,$response); // execute CURL command
print_r($response); // print array response

?>

It's surprising that CURL version 7.70.0 is perfectly working on an old operating system like Windows 7, 32-bit architecture.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM