如何通过php cURL发送2D数组 [英] How to Send 2D array through php cURL
本文介绍了如何通过php cURL发送2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用一个分布式系统,其中php应用程序发送一个帖子请求到一个python应用程序。
I'm working with a a distributed system where a php app sends a post request to a python app.
我的代码非常简单:
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
我有一个2d php数组,看起来像:
I have a 2d php array that looks like:
array(3) { [0]=> array(2) { ["a"]=> 'aaa' ["token"]=> string(55) "146bf00b2cb8709" } [1]=> array(2) { ["a"]=> string(52) "bbb" ["token"]=> string(55) "146bf00b2cb96e74302" } [2]=> array(2) { ["a"]=> string(52) "ccc" ["token"]=> string(55) "146bf00b2cb96e6c422417" } }
我想通过php curl传输,
I want to transmit this via php curl, but I'm not sure how to do this in a way that is decodable on the other end in python.
推荐答案
php
php
// 2d array
$arr = array(array(1,2,3),array(4,5,6),array(7,8,9));
// 2d array into json
$json = json_encode($arr) // [[1,2,3],[4,5,6],[7,8,9]]
send($json)
python
python
import json
r = request.body # receives request from php
json = json.loads(r)
print json[0] # [1,2,3]
这篇关于如何通过php cURL发送2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文