问题描述
我发现如果我尝试PHP POST curl,postvars就可以发送了。一旦我添加了内容类型的httpheader: application / json
,postvars就不再发生了。我已经将postvars作为JSON字符串和查询字符串进行了尝试。
I've found that if I try a PHP POST curl, the postvars are sent fine. Once I add the httpheader of content-type: application/json
the postvars don't go across any more. I've tried the postvars as a JSON string and as a query string.
显示一些代码:
$ch = curl_init();
$post = json_encode(array('p1' => 'blahblahblah', 'p2' => json_encode(array(4,5,6))));
$arr = array();
array_push($arr, 'Content-Type: application/json; charset=utf-8');
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
curl_setopt($ch, CURLOPT_URL, 'https://example.com/file.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_exec($ch);
curl_close($ch);
推荐答案
一个真正的HTTP Post,它被分解成一个数组PHP自动化必须使用name = value对进行格式化,在PHP中执行此操作的最佳方法是使用http_build_query函数。
A real HTTP Post, that gets broken into an array automagically by PHP must be formatted in name=value pairs, the best way to do this in PHP is using the http_build_query function.
有一个例子可以在PHP手册中使用curl:
There is an example that works in the PHP Manual using curl:
你正在做的是'RAW'帖子,请看另一个问题:
What you're doing is a 'RAW' post, see this other question:
获取RAW Json数据的快速代码段。
A quick snippet to get the RAW Json data.
<?php
print_r(json_decode(file_get_contents('php://input')));
这篇关于“application / json”的类型防止发送变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!