0

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!

2 Answers2

0

it seems like you're doing everything correctly there (except the horrible indenting on line 10+11 making it look like you're missing a ) when you're actually not), you're just lacking error-checking code, to debug that, try:

$stderrh=tmpfile();
curl_setopt_array($ch,[CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh]);
$result = curl_exec($ch);
rewind($stderrh); // https://bugs.php.net/bug.php?id=76268
var_dump(stream_get_contents($stderrh),$result);

the verbose log should tell you what the problem was, what does it say?

(also you're missing the <?php at the start and you might wanna add var_dump($result); at the end. also to speed things up you can add CURLOPT_ENCODING=>'' to make curl use compression for the transfer, if the response is compressible (like JSON or HTML or text), that usually speeds things up)

hanshenrik
  • 19,904
  • 4
  • 43
  • 89
0

Try this code.

    $ch = curl_init( );
    $data = array("Content" => "<CONTENT of the message>");
    $headers = array(
    'Content-Type: application/json'
    );

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt ( $ch, CURLOPT_URL, $url );       
    curl_setopt ( $ch, CURLOPT_POST, 1 );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data);
    // Allow cUrl functions 20 seconds to execute
    curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
    // Wait 10 seconds while trying to connect
    curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
    $output = array();
    $output['server_response'] = curl_exec( $ch );
    $curl_info = curl_getinfo( $ch );
    $output['http_status'] = $curl_info[ 'http_code' ];
    $output['error'] = curl_error($ch);
    curl_close( $ch );
    return $output;
BCM
  • 665
  • 5
  • 20