Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

how to send php multi-dimensional array from curl post/get and get it with python and convert to multi-dimensional dict:

requirements: web server with php and cgi support

debian gnu/linux instalation:

su
aptitude install python-pip php5-curl
pip install querystring-parser
exit

curl.class.php

<?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) && count($params)>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)>0)
            {
                foreach($curl_opts as $key => $value)
                    curl_setopt($ch, $key, $value);
            }           
        }                               
        $result = curl_exec($ch);
        return $result; 
    }

    public static function setParams($array, $type = 'get')
    {
        $txt = '';
        if(count($array)>0)
        {
            if($type=='get')
                $txt.= '?';
            $txt.= http_build_query($array);            
        }
        return $txt;
    }   
}
?>

index.php

<?php
include('curl.class.php');
$vars = array
(
    'aitxitxe' => array
    (
        'aita' => array
        (
            'semie' => 'iban',
            'alabie' => 'maite'
        )
    )
);

//echo curl::getURL('http://localhost:81/python/request.py', $vars, 'post');
echo curl::getURL('http://localhost:81/python/request.py', $vars, 'get');
?>

request.py

#!/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 > 0:
            text+= "&"
        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()

in the browser: http://localhost/index.php formated output:

{
  'aitxitxe':
  {
    'aita':
    {
      'alabie': 'maite',
      'semie': 'iban'
    }
  }
}

thanks for: Django, Python: Is there a simple way to convert PHP-style bracketed POST keys to multidimensional dict?

improvements allowed, thank you ;)

share|improve this question
    
There doesn't seem to be a question here, just a bunch of code. –  rjmunro Mar 9 '12 at 11:57

1 Answer 1

Both languages support JSON for handling this kind of data.

http://php.net/manual/en/book.json.php

http://docs.python.org/library/json.html

It's probably a good idea to use those libraries.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.