0

I was trying to write the same post request via php as i did in python. In python it works well, while in php it does not even reach the server at all. This is php 7.4. Also I can access the server http://someip:2080/api/ via browser. So http://someip:2080/api/ is a public IP, I can even access via browser, so it should work from anywhere.

PHP code:

$payload = array(
'data' => 'data0',
);

// Setup cURL
$ch = curl_init('http://someip:2080/api/');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json'
),
CURLOPT_POSTFIELDS => json_encode($payload)
));

$response = curl_exec($ch);


if($response === FALSE){
   die(curl_error($ch));
}

and python code:

import requests
import json
headers = {'Content-Type': 'application/json'}
resp = requests.post("http://someip:2080/api/" , data = json.dumps(allparams),  headers=headers)
thepara = resp.json() 

I have no idea what is wrong I tried around 10 ways to make post request from php and nothing worked.

4
  • 1
    "it does not even reach the server at all" then you probably have a network problem.
    – Sammitch
    Commented Jul 5, 2023 at 23:45
  • I tried from python and the same ip and you are correct it does not work. So it is some kind of network error. I just do not know what would is the cause, because I am able to access this URL from browser.
    – Brana
    Commented Jul 6, 2023 at 19:19
  • I guess it is problem that port 2080 is likely already used by cpanell:
    – Brana
    Commented Jul 6, 2023 at 19:37
  • 2080 CalDAV and CardDAV (SSL)
    – Brana
    Commented Jul 6, 2023 at 19:38

1 Answer 1

0

The problem was that the port 2080 was already occupied on a cpanel. So the solution is to :

  1. Change port to lets say 9999 which is not occupied
  2. Allow the port in WHM firewall, under IPv4 Port Settings (in WHM - ConfigServer Security & Firewall ) or tell admin to do this for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.