在PHP POST上发送复杂的json数据
我需要调用包含复杂实体数据的post方法:在PHP POST上发送复杂的json数据,php,curl,Php,Curl,我需要调用包含复杂实体数据的post方法: { "client": { "clientId": "yourcompanyname", "clientVersion": "1.5.2" }, "threatInfo": { "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"], "platformTypes": ["WINDOWS"], "thr
{
"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/"}
]
}
}
我发现了使用curl和stream_context_create的不同示例,但所有示例都只是使用数组作为主体数据。在我的示例中,没有一个具有复杂的json结构。如何做到这一点?试试:
$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);
您是向服务器发送还是从服务器发送?向服务器发送。这是关于请求而不是响应的问题这不是同一个问题吗?难道你不能对你的数据进行json_编码吗?@OleAlbers你是否真的尝试过遵循你发现的任何一个例子?还是你以为他们不会做你想做的事?如果您确实尝试过,请在问题中包括您的尝试以及结果。我的问题只是,我没有数组所要求的简单的平面键/值对结构,而是层次数据。