2

I am posting an array converted to JSON string with cURL like below. However on the receiving end, I get an empty array.

function postFunction ($data_h) {

    $c = curl_init();

    curl_setopt_array($c, array(
            CURLOPT_URL => 'https://domain.tld/script.php',
            CURLOPT_HTTPHEADER => array('Content-Type:application/json'),
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => json_encode($data_h), 
            CURLOPT_RETURNTRANSFER => true
        ) 
    );

    return curl_exec($c);
    curl_close($c);
}


$setup = [
    'sms' => [
        'number' => '+39XXXXXXXX',
        'message' => 'Sample msg 123...',
    ]
];


print_r( postFunction($setup) );

Instead of $_POST I've tried using var_dump($HTTP_RAW_POST_DATA) as I saw on another question on Stack—but all I get with it is a NULL.


This is what I get when I return curl_getinfo($c):

It doesn't even adjust the content type.

Array
(
    [url] => https://domain.tld/script.php
    [content_type] => text/html; charset=utf-8
    [http_code] => 200
    [header_size] => 140
    [request_size] => 593
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.127732
    [namelookup_time] => 0.013128
    [connect_time] => 0.013257
    [pretransfer_time] => 0.106738
    [size_upload] => 463
    [size_download] => 5
    [speed_download] => 39
    [speed_upload] => 3624
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 0.125601
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => xx.xx.xx.xx
    [certinfo] => Array
        (
        )

)

At the end I want to turn the data into an object with json_decode() however I first need to get the data.

Any thoughts on what I'm doing incorrectly here?

Nikk
  • 7,384
  • 8
  • 44
  • 90
  • @u_mulder I have it as `$content = curl_exec($c); return $content;` I was just cleaning the code for here. – Nikk Dec 11 '16 at 19:24
  • Does `curl_error` show anything? – scrowler Dec 11 '16 at 19:24
  • 2
    `curl_close($c);` never happens because the function execution completes when it reaches the `return` statement. – axiac Dec 11 '16 at 19:24
  • @RobbieAverill Nothing. Empty. – Nikk Dec 11 '16 at 19:27
  • @axiac So I never should use `curl_close()`? – Nikk Dec 11 '16 at 19:30
  • @Borsn the comment means capture the content, close the connection and _then_ return the result. The way your code is right now means the cURL connection is never closed. E.g. `$result = curl_exec($c); curl_close($c); return $result;` – scrowler Dec 11 '16 at 19:35
  • @RobbieAverill `Just done that...hasn't changed anything.` I've added additional stuff to the question, please check it out. – Nikk Dec 11 '16 at 19:38

1 Answers1

1

In your code you are indicating the content-type to be json, but your POST data isn't really all json. You can use

file_get_contents("php://input")

To get your data, instead of $_POST

Some more info here and here

Community
  • 1
  • 1
  • That worked...thanks. How do I make sure it sends as JSON though? Why isn't `CURLOPT_HTTPHEADER => array('Content-Type:application/json')` doing its job? – Nikk Dec 11 '16 at 20:02
  • @Borsn, sorry I was unable to find a better source, so let me try some superficial explaining: $_POST expects something like key1=value1&key2=value2..., you can post a json, but never read it with $_POST Check this out: https://www.w3.org/TR/html401/interact/forms.html#h-17.13 Glad I was able to help! – Victor Hugo Dec 11 '16 at 20:37
  • Yeah thats fine...I understand the $_POST part. Just that cURL is not sending as `Content-Type: application/json` is the problem. Is there any way to fix that? – Nikk Dec 11 '16 at 20:43