Skip to content Skip to sidebar Skip to footer

How To Send 2D Array Through Php CURL

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

Solution 1:

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]

Solution 2:

curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

Will encode your request as an HTTP query and transform your data into a multipart/form-data.

The python application must be an HTTP server and be able to receive this request. The decoding will be done by the HTTP framework/module.


Post a Comment for "How To Send 2D Array Through Php CURL"