I'm trying to POST a JSON payload using PHP, but can't get it working. I have the following CURL command that works on Shell as well as the following Python code that also works, but need PHP implementation of the same functionality.
CURL on Shell:
curl -H "Content-Type: application/json" -X POST -d '{"Content":" <CONTENT OF THE MESSAGE> "}' <URL of the node receiving POST data>
Python:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
headers = {
'Content-type': 'application/json',
}
auth = '<URL>'
line1 = '<CONTENT of message>'
data = '{"Content":"%s"}' % (line1)
response = requests.post(url = auth, headers = headers, data = data)
What I have so far in PHP (not working):
$data = array("Content" => "<CONTENT of the message>");
$data_string = json_encode($data);
$ch = curl_init('<URL>');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
Any and all help much appreciated in advance!