php array multi-dimensional bat python dict bihurtu

Ba hamen gabilz barriro python-egaz pellikan, oingutan php-tik curl bidez botaku array multi-dimensional bat post edo get bidez eta python-egaz hartun.

Okin biharrekuek: python, web zerbitzaridxe php-gaz eta cgi-gaz

Paketiek instala:

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

curl.class.php artxibue:

<?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 artxibue:

<?php
include('curl.class.php');

$vars = array
(
	'aitxitxe' => array
	(
		'aita' => array
		(
			'semie' => 'iban',
			'alabie' => 'maite'
		)
	)
);

echo curl::getUrl('http://localhost/python/request.py', $vars);
?>

oin python-en partie: request.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, cgi

# request parametruek hartu eta dic baten barruen sartu

params = {}
form = cgi.FieldStorage()

for k in form.keys():
    params[k] = form[k].value

print "Content-type: text/plain\n"
print params

sys.exit()

oin guez: http://localhost/index.php -ra eta hau da erantzune:

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

oops, ikusten dun moduen sortu den dict-e ezta multi-dimentsionala unidimentsionala baño, hori konponduteko erabilku: querystring-parser

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys, cgi
from querystring_parser import parser

# request parametruek hartu eta dic baten barruen sartu

params = {}
form = cgi.FieldStorage()

for k in form.keys():
    params[k] = form[k].value
    

# dict -e query-string-era pasateko funtziñue

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 parametruek query-string-era pasa

params_text = dict2querystring(params)

# query-string-e dict-era pasa

post_dict = parser.parse(params_text)

print "Content-type: text/plain\n"
print post_dict

sys.exit()

Eta oingo erantzune, oin bai :)

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

Eskerrak: http://stackoverflow.com/questions/1890013/django-python-is-there-a-simple-way-to-convert-php-style-bracketed-post-keys-t

Utzi erantzuna

Zure e-posta helbidea ez da argitaratuko. Beharrezko eremuak * markatuta daude