1

I get the following Warning:
Warning: http_build_query(): Parameter 1 expected to be Array or Object.

I know what this error represents and know why i get it, but I do not know how to fix it because the code works like a charm, so maybe you guys can help me out?

I curl a url with the following CURLOPT_POSTFIELDS:
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query(json_encode($data)));

The vardump of json_encode($data) =

string(57) {"transporterCode":"TNT","trackAndTrace":"3SAOLD1234567"}"

Hence the error (it is a string), however I am not allowed to change anything because then the API call will fail. Is there anything I can do (besides supress the error) to fix it?

/love

Grumpymuppet

4
  • http_build_query([json_encode($data])? Commented Jun 11, 2020 at 10:24
  • 1
    You should pass first parameter array try this curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($data)); Commented Jun 11, 2020 at 10:27
  • You would expect that to work yes Forge Web Design, however it does not accept it without the json_encode. This is the message: The supplied body does not meet the required specifications. Please check the message structure for errors. Commented Jun 11, 2020 at 12:16
  • How would that work Berto99 with twice opening ( and once closing )? Commented Jun 11, 2020 at 12:19

1 Answer 1

1

For anyone that has the same issue. For me it was a combination of the required post method (PUT) and the http_build_query() who was messing that up.

I ended up using:

       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
       curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

And that fixed it.

Good luck <3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.