문제

I am using Python to query a WordPress.org API using urllib. WordPress.org, in turn, decides to respond with an encoded PHP StdClass object. It would be great if I were using PHP, but I must use Python. Any idea how to take this and turn it into a dictionary (without the s:10:"slijekgdds" prefixes)?

apiUrl = "http://api.wordpress.org/themes/info/1.0/"
body = urllib.urlencode({
    'action': "query_themes",
    'request': {
        "browse": "featured", 
        'fields': {
            'download_link': True,
            'preview_url': False,
            'screenshot_url': False,
            'description': False,
            'sections': False,
            'screenshots': False,
            'downloaded': False,
            'screenshot_count': False
        }
    }
}, True)

request = urllib.urlopen(apiUrl, body)
response = request.read(200)

This is returning (truncated):

O:8:"stdClass":2:{
    s:4:"info";
    a:3:{
        s:4:"page";
        i:1;
        s:5:"pages";
        i:41;
        s:7:"results";
        i:610;
    }
    s:6:"themes";
    a:15{
        i:0;
        O:8:"stdClass":10:{
            #BLAH BLAH
        }
    }
}
도움이 되었습니까?

해결책

Based on this, which you'll have to install with pip or somesuch, I'm guessing this is what you want:

from phpserialize import loads, phpobject

python_object = loads(php_serialized_string, object_hook=phpobject)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top