A
A
Alexander Borisovich2013-02-22 13:38:58
PHP
Alexander Borisovich, 2013-02-22 13:38:58

How to parse such a POST response? Online Store API

The online store has its own API. On request, it displays the following in the browser:

HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2013 10:34:03 GMT
Server: Apache/2.2.8 (CentOS)
X-Powered-By: PHP/5.3.20
X-Powered-By: PleskLin
Content-Length: 2497
Content-Type: text/html

<pre>
Array
(
    [id] => 41
    [name] => TRAXXAS
    [site] => www.traxxas.com
    [description] => 
    [imagename] => imglib/icon_brand/201112141035__79235c09.jpg
    [subdomain] => traxxas
)
</pre>



Question. How can they make an array without any regular expressions?
— PS. It looks like this is a bug on the site - debug information. Thanks to all.

Answer the question

In order to leave comments, you need to log in

11 answer(s)
A
Alexander Borisovich, 2013-03-06
@Alexufo

Support responded. She said that there is a field for the response of the script. There you need to insert the address of our script, to which the post response will come. By default, there was just a link in which print_r () was written;

G
gaelpa, 2013-02-22
@gaelpa

Write to the developers of this store the question “What did you mean by THIS ??” there is probably a script that gives the answer in the normal form, this one looks a lot like a debugging interface.

S
Sergey Cherepanov, 2013-02-22
@fear86

Most likely to disassemble by hand.

A
Alexey Akulovich, 2013-02-22
@AterCattus

$a = explode("\n", $a);
$a = array_map('trim', $a);
// выборка строк, начинающихся на "[" и содержащих "] => "
$a = array_filter(
    $a,
    function($i) {
        return (0 === strpos($i, '[')) && (false !== strpos($i, '] => '));
    }
);

$result = array();
// разделение строк по  "] => " и наполнение итогового словаря
$a = array_map(
    function($i) use(&$result) {
        $i = explode('] => ', ltrim($i, '['), 2);
        $result[$i[0]] = $i[1];
    },
    $a
);

print_r($result);

You can merge into one iteration if you want.

U
Urvin, 2013-02-22
@Urvin

Without regex - by hand.
With regular expressions - you can print_r_reverse if the answer is unpredictable

J
Jonh Doe, 2013-02-22
@CodeByZen

With regular expressions it is much easier. Everything is done in 4 lines. Worth paying attention.
If the answer is exactly like yours. Then the following code will do:
in the $post variable what the server returned

$arr=explode("<pre>",$post);
preg_match_all("/\n\s+?\[([^\]]+)\]\s=>\s([^\n]+)/is", $arr[1],$m);
foreach($m[1] as $k=>$v) { $out[$v]=$m[2][$k]; }
print_r($out);

As a result, we get the following array:
Array
(
    [id] => 41
    [name] => TRAXXAS
    [site] => www.traxxas.com
    [imagename] => imglib/icon_brand/201112141035__79235c09.jpg
    [subdomain] => traxxas
)

E
egorinsk, 2013-02-22
@egorinsk

It looks like someone is debugging the code and temporarily put in var_dump(). Are you sure this is not a temporary problem?
If not temporary. then parse, apparently, with the help of regular expressions and (possibly) html_entities_decode.

A
Alexey Zhurbitsky, 2013-02-22
@blo

Try specifying a different Content-Type and Accept in the request, for example
application/json

E
edogs, 2013-02-22
@edogs

On a request in the browser, does it issue it? No proxy or anything?
Not so long ago, we encountered a similar one and there was also HTTP / 1.1 100 Continue.
But when they began to drag not the first part (we won’t remember now why only the 1st part was given, but the changes on our part led to a normal result in the end), but the full answer, then the remainder immediately after var_dump was a normal full-fledged xml response.
In general, check whether you really get the answer in full, if there is anything after this print_r.

X
xmoonlight, 2013-02-22
@xmoonlight

Just write in the site's TP that it would be nice to have two versions: Production and Development for API development.

P
pletinsky, 2013-02-22
@pletinsky

I am not a php developer. As far as I understand, this is a piece of php code, which is an object, instead of representing it in json.
That is, you need to finish the work of these dudes who did not finish the necessary code for serializing the object in json.
1) Pull the contents of the pre tag into a string.
2) Place it inside the following string: "json_encode({Your array})" and add another output of the encode result to the file.
3) Execute this code with php interpreter.
4) Read the resulting json.
I think you can greatly simplify the work using execute or even better eval and giving them a string representation of this array as input. Then get the object right away.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question