Note that there are some explanatory texts on larger screens.

plurals
  1. POphp array multi-dimensional to python multi-dimensional dict
    primarykey
    data
    text
    <p>how to send php multi-dimensional array from curl post/get and get it with python and convert to multi-dimensional dict:</p> <p>requirements: web server with php and cgi support</p> <p>debian gnu/linux instalation:</p> <pre><code>su aptitude install python-pip php5-curl pip install querystring-parser exit </code></pre> <p><strong>curl.class.php</strong></p> <pre><code>&lt;?php class curl { public static function getURL($url, $params = array(), $type = 'get', $curl_opts = array()) { if(!function_exists('curl_init')) die('curl module missing'); $type = trim(strtolower($type)); if(is_array($params) &amp;&amp; count($params)&gt;0) $params = self::setParams($params); $ch = curl_init(); if($type=='post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_URL, $url); } else curl_setopt($ch, CURLOPT_URL, $url.$params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // set curl param options if(is_array($curl_opts)) { if(count($curl_opts)&gt;0) { foreach($curl_opts as $key =&gt; $value) curl_setopt($ch, $key, $value); } } $result = curl_exec($ch); return $result; } public static function setParams($array, $type = 'get') { $txt = ''; if(count($array)&gt;0) { if($type=='get') $txt.= '?'; $txt.= http_build_query($array); } return $txt; } } ?&gt; </code></pre> <p>index.php</p> <pre><code>&lt;?php include('curl.class.php'); $vars = array ( 'aitxitxe' =&gt; array ( 'aita' =&gt; array ( 'semie' =&gt; 'iban', 'alabie' =&gt; 'maite' ) ) ); //echo curl::getURL('http://localhost:81/python/request.py', $vars, 'post'); echo curl::getURL('http://localhost:81/python/request.py', $vars, 'get'); ?&gt; </code></pre> <p><strong>request.py</strong></p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- import sys, cgi from querystring_parser import parser # request to dict form = cgi.FieldStorage() params = {} for k in form.keys(): params[k] = form[k].value # dict to query-string def dict2querystring(dict): text = '' count = 0 for i in dict: if count &gt; 0: text+= "&amp;" text+= str(i) + "=" + str(dict[i]) count += 1 return text # request to query-string params_text = dict2querystring(params) # query-string to multi-dimensional dict post_dict = parser.parse(params_text) print "Content-type: text/plain\n" print post_dict sys.exit() </code></pre> <p>in the browser: <strong><a href="http://localhost/index.php" rel="nofollow noreferrer">http://localhost/index.php</a></strong> formated output:</p> <pre><code>{ 'aitxitxe': { 'aita': { 'alabie': 'maite', 'semie': 'iban' } } } </code></pre> <p>thanks for: <a href="https://stackoverflow.com/questions/1890013/django-python-is-there-a-simple-way-to-convert-php-style-bracketed-post-keys-t">Django, Python: Is there a simple way to convert PHP-style bracketed POST keys to multidimensional dict?</a></p> <p>improvements allowed, thank you ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload