使用Curl发布JSON [英] Post JSON using Curl

查看:139
本文介绍了使用Curl发布JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个JSON,我可以收到使用php://输入,我需要发布回不同的URL,但我不知道如何格式化。以下是我收到的方式:

I am receiving a JSON which I can receive using php://input and I need to post it back to a different URL, but I'm not sure how to format it. Here's how I receive it:


$ updates =
file_get_contents(php:// input);

$updates = file_get_contents("php://input");

我可以对它进行json_decode,然后解析出数组,以便它适合一个正常的类POST请求,如hello = world& stack = overflow等。但是它是可能传递JSON只是一个curl post像这样

I could json_decode it and then parse out the array so that it would fit a normal POST-like request such as hello=world&stack=overflow etc. But is it possible to pass the JSON just into a curl post like this:

curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $updates);

然后客户端可以只是输入p​​hp://再次获取数据?我问的原因是因为客户端说他没有收到数据,可能是他没有正确设置。我注意到,当你这样做通过命令行,你必须在命令中包括-H,以便数据解释为正常的POST,也许有一个libcurl CURLOPT我需要设置?

and then the client could just php://input to grab the data again? The reason I ask is because the client says he is not receiving the data, it may be he doesn't have it set up correctly. I've noticed when you do something like this through a command line you have to include -H in the command so that the data is interpretted as normal POST, perhaps there's a libcurl CURLOPT I need to set?

谢谢!

推荐答案

好的,我已经想出了这一个!

Well folks, I have figured this one out!

要将帖子作为不同的内容类型发送(即.. application / json或text / xml),请添加此setopt调用

To send a post as a different content-type (ie.. application/json or text/xml) add this setopt call


curl_setopt($ ch,
CURLOPT_HTTPHEADERS,array('Content-Type:
application / json'));

curl_setopt($ch, CURLOPT_HTTPHEADERS,array('Content-Type: application/json'));

这篇关于使用Curl发布JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆