繁体   English   中英

php数组多维到python多维dict

[英]php array multi-dimensional to python multi-dimensional dict

如何从curl post / get发送php多维数组并使用python获取并转换为多维dict:

要求:支持php和cgi的web服务器

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()

在浏览器中: http://localhost/index.php格式化输出:

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

谢谢: Django,Python:有没有一种简单的方法将PHP风格的括号内的POST键转换为多维dict?

允许改进,谢谢;)

两种语言都支持JSON来处理这种数据。

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

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

使用这些库可能是个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM