-1

I need to call a post method containing complex body data:

{
    "client": {
      "clientId":      "yourcompanyname",
      "clientVersion": "1.5.2"
    },
    "threatInfo": {
      "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
      "platformTypes":    ["WINDOWS"],
      "threatEntryTypes": ["URL"],
      "threatEntries": [
        {"url": "http://www.urltocheck1.org/"},
        {"url": "http://www.urltocheck2.org/"},
        {"url": "http://www.urltocheck3.com/"}
      ]
    }
  }

I found different examples using curl and using stream_context_create but all examples simply use an array as body data. None had a complex json-structure as needed in my example. How can this be achieved?

7
  • Are you sending to the server or from the server? Commented Feb 19, 2019 at 19:00
  • TO the server. It is about the request not the response Commented Feb 19, 2019 at 19:00
  • Isn't this the same question as stackoverflow.com/questions/11079135/…? Can't you just json_encode your data? Commented Feb 19, 2019 at 19:02
  • 1
    @OleAlbers Did you actually try to follow any of the examples that you found? Or did you just assume that they wouldn't do what you wanted? If you did try, please include your attempts in your question along with the results. Commented Feb 19, 2019 at 19:05
  • My problem simply is that I do not have a simple flat Key/Value-Pair Structure as required by arrays, but hierarchical data. Commented Feb 19, 2019 at 19:10

1 Answer 1

1

Try :

$ch=curl_init($url);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data) );
curl_setopt($ch, CURLOPT_POST, true);
// not necessarily but ... 
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($payload))
);

$result = curl_exec($ch);
curl_close($ch);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.