This question already has an answer here:

I'm using this code for my api application.

$url = 'http://example.com/api/';
$ch = curl_init($url);
$jsonData = array(
    'username' => 'MyUsername',
    'password' => 'MyPassword'
$jsonDataEncoded = json_encode($jsonData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

//Execute the request
$result = curl_exec($ch);

I can allow only POST method request and check content type set to application/json. How can I allow access API only using HTTPS?


        if(strcasecmp($contentType, 'application/json') != 0){

        $decoded = json_decode($content, true);
            throw new Exception('Received content contained invalid JSON!');


marked as duplicate by marekful, Jay Blanchard php Feb 5 at 12:53




Added this code in receive.php.

I have send.php (on server without https connection) and receiving.php (on server with https connection)

The results is: 'https'

Instead I need to check connection from send.php

   $https = !empty($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'on') === 0 ||
            !empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
                strcasecmp($_SERVER['HTTP_X_FORWARDED_PROTO'], 'https') === 0;

    echo ($https) ? 'https://' : 'http://';
