POST JSON数据通过CURL并抓取它 [英] POST JSON data via CURL and grabbing it

查看:204
本文介绍了POST JSON数据通过CURL并抓取它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递一个json数据作为param的cURL POST。

I am trying to pass a json data as param for cURL POST. However, I am stuck at grabbing it and saving it on db.

cURL文件:

$data = array("name" => "Hagrid", "age" => "36");                                                                    
$data_string = json_encode($data);                                                                                   

$url = 'http://localhost/project/test_curl';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
                                    'Content-Type: application/json')                                                                                           
                                    );                       
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                                                     

$result = curl_exec($ch);  

//based on http://www.lornajane.net/posts/2011/posting-json-data-with-php-curl

test_curl文件:

test_curl file:

    $order_info = $_POST; // this seems to not returning anything

    //SAVE TO DB... saving empty...

我错过了什么? Weew ....

What did I miss? Weew....

推荐答案

你将数据作为原始JSON发送到正文中,它不会填充 $ _ POST 变量。

You are sending the data as raw JSON in the body, it will not populate the $_POST variable.

您需要执行以下两项操作之一:

You need to do one of two things:


  1. 您可以将内容类型更改为填充 $ _ POST 数组的内容类型

  2. 可以读取原始数据。

  1. You can change the content type to one that will populate the $_POST array
  2. You can read the raw body data.

如果您能够控制通信的两端,我会建议使用选项二,将请求主体大小降低到最小并且随时间节省带宽。 (编辑:我没有真正强调这里的带宽量可以忽略不计,每个请求只有几个字节,这只是一个有效的关注是非常高的交通环境。但我仍然建议选项二

I would recommend option two if you have control over both ends of the communication, as it will keep the request body size to a minimum and save bandwidth over time. ( I didn't really emphasize here that the amount of bandwidth it will save is negligible, only a few bytes per request, this would only be a valid concern is very high traffic environments. However I still recommend option two because it is the cleanest way)

在您的 test_curl 文件中,执行以下操作:

In your test_curl file, do this:

$fp = fopen('php://input', 'r');
$rawData = stream_get_contents($fp);

$postedJson = json_decode($rawData);

var_dump($postedJson);

如果要填充 $ _ POST 变量,您需要更改将数据发送到服务器的方式:

If you want to populate the $_POST variable, you will need to change the way you send the data to the server:

$data = array (
  'name' => 'Hagrid',
  'age' => '36'
);

$bodyData = array (
  'json' => json_encode($data)
);
$bodyStr = http_build_query($bodyData);

$url = 'http://localhost/project/test_curl';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Content-Type: application/x-www-form-urlencoded',
  'Content-Length: '.strlen($bodyStr)
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyStr);

$result = curl_exec($ch);

原始的未解码JSON现在可以在 $ _ POST ['json ']

The raw, undecoded JSON will now be available in $_POST['json'].

这篇关于POST JSON数据通过CURL并抓取它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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