0

I have the following problem. This data in this structure

$transfer = {stdClass} [4]
    module = "Member"
    action = "create"
    token = null
    params = {stdClass} [3]
        username = "Test"
        email = "test@test.com"
        password = "Test"

has to be send vi REST to the REST Server.

Therefore I encode the data with json_encode ($object). The decoded object looks like this:

{"module":"Member","action":"create","token":null,"params":{"username":"Test","email":"test@test.com","password":"Test"}}

For testing purpose I decode the encoded result to see if everything works fine. Which gives me the object correct back.

When I transfer the data via curl, the server receives this json_encoded data:

{"module":"Member","action":"create","token":null,"params":{"username":"Test","email":"test@test.com" = ""

And finally the json_decode($request) ands up with the following error:

json_decode() expects parameter 1 to be string, array given

The curl code for this is:

$curl = curl_init($url);

// prepare curl for Basic Authentication
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

$curl_post_data = json_encode($postdata);
$test = json_decode($curl_post_data); // for testing

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); <= $postdata = $transfer the object/array mentioned above
$curl_response = curl_exec($curl);
curl_close($curl);

What's wrong? Why it is not possible to decode the encoded data after the curl exec to the REST Server?

Perino
  • 608
  • 9
  • 30
  • Please check Code :- $object = '[{"module":"Member","action":"create","token":null,"params":{"username":"Test","email":"test@test.com","password":"Test"}}]'; $data = json_decode($object); string problem – sunny bhadania Apr 12 '18 at 05:15
  • What do you mean with check code Sunny? I putted also the curl statement in addition now. – Perino Apr 12 '18 at 05:39

2 Answers2

0

This is the magic solution for that problem:

json_decode(key($object), true);
Perino
  • 608
  • 9
  • 30
-1

From my understanding you are sending $curl_post_data which is a json_encoded string in CURLOPT_POSTFIELDS.

CURLOPT_POSTFIELDS accepts array not a string.

So you should pass $curl_post_data['data'] = json_encode($postdata);

and receive data json_decode($request['data'])

You are facing error(json_decode() expects parameter 1 to be string, array given) because $request is blank as you passed the string

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
  • Minus rating is not from me. I don't know why this happend, there is no comment. Please stay tuned to this topic. – Perino Apr 12 '18 at 05:41
  • i want to answer question that's why i asked for curl data.And i answered it in edit.I can't comment because i am new here that's why i asked in answer – Dewanshu Sharma Apr 12 '18 at 06:05
  • 2
    @DewanshuSharma you should answer some different questions that don't require you to misuse the answer field till you get the rep to comment. – Robert Longson Apr 12 '18 at 06:07
  • $curl_post_data['data'] is not working, the rest call won't be fired with that. – Perino Apr 12 '18 at 06:11
  • you can pass either array or url_encoded string like this 'data='.urlencode(json_encode($postdata)) and now you can get $postdata by $request['data'] or add this curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($postdata))); – Dewanshu Sharma Apr 12 '18 at 06:17