How to Send 2D array through php cURL

2024/11/20 2:47:07

I'm working with a a distributed system where a php app sends a post request to a python app.

My code is pretty straight forward:

$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);

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" } }

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.

Answer

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

import json
r = request.body # receives request from php
json = json.loads(r)
print json[0] # [1,2,3]
https://en.xdnf.cn/q/119880.html

Related Q&A

Internally, is asyncio run_forever() basically a while True loop?

python asyncio run_forever or while True is similar but it is a "should I do this..." question. I am more trying to understand if the internals of python asyncio is basically a while True:...…

How do I switch from python 2.6 to 2.7 by default

How do I switch from python 2.6 to 2.7 by defaultls -l /usr/bin/python* lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python -> python2.6 lrwxrwxrwx 1 root root 9 Jan 27 12:36 /usr/bin/python…

How to make a flat list from nested lists? [duplicate]

This question already has answers here:Flatten an irregular (arbitrarily nested) list of lists(54 answers)Closed 1 year ago.Given a nested list of integers, implement an iterator to flatten it. Each el…

Python 2.7: import performance

currently, I am importing bunch of .py files scattered across the file system via: def do_import(name):import impfp, pathname, description = imp.find_module(name)with fp:return imp.load_module(name, fp…

change first line of a file using bash

I need to change a single line in a file. It is always in the first line of the file. It looks like:h\tn0 n1 n2 n3 n4 n5 n6 n7 n8 n9 hilu cjt 1 1000000there is a tab in all gaps except …

1 to 2 matching in two dataframes with different sizes in Python/R

please help me with this problem Ive been struggling all day lol, solution in either Python or R is fine! Please help Im really stuck!!! I have two dataframes - df1 has 44 rows, df2 has 100 rows, they …

Can someone fix this? pip install intents

Im not sure why this is failing but I need help to fix it please Ive been trying to fix it for about 2 hours now and cant figure it out. I have tried to restart my computer, look it up on google etc. b…

Calculating Average Performance Measures in M/M/1 System Using SimPy

I am seeking to calculate the average waiting times, and average service times in the following M/M/1 queueing system, but I am not able to calculate the averages. It writes down to the console each cu…

Python: Whats the difference between import X and from X import *? [duplicate]

This question already has answers here:Use import module or from module import?(23 answers)Closed 7 years ago.I use to think both are equal until I tried this:$python Python 2.7.13 (default, Dec 17 20…

Python iterate through pixels of image

Im trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Imagew=100 h=200 im=im.resize((w,h), Im…