JSON Openload PHP

Asked

Viewed 169 times

1

Can anyone help me? I have the following json url: https://api.openload.co/1/file/info?file=6PHaY9bqRYc

I wanted to know how to list in php each part that has in the json leaving type

Name: name you have in json Id: id you have in json

And for a json like that:

{"status":200,"msg":"OK","result":{"ticket":"valorticket","captcha_url":"valorcaptcha","captcha_w":160,"captcha_h":70,"wait_time":0,"valid_until":"2018-05-11 15:55:44"}}

What would code look like? I have a json url since then, I tried changing the url and not the result, only errors

2 answers

3

Using curl or file_get_contents and then using json_decode.

The result of this:

{"status":200,"msg":"OK","result":{"6PHaY9bqRYc":{"id":"6PHaY9bqRYc","status":200,"name":"big_buck_bunny_720p_1mb.mp4","size":"1055726","sha1":"5b4395dabecd6308ec82bb4f1626f41c40e64654","content_type":"video\/mp4","cstatus":"1"}}}

With json_decode will be more or less these:

Array
(
    [status] => 200
    [msg] => OK
    [result] => Array
        (
            [6PHaY9bqRYc] => Array
                (
                    [id] => 6PHaY9bqRYc
                    [status] => 200
                    [name] => big_buck_bunny_720p_1mb.mp4
                    [size] => 1055726
                    [sha1] => 5b4395dabecd6308ec82bb4f1626f41c40e64654
                    [content_type] => video/mp4
                    [cstatus] => 1
                )

        )

)

Then you’ll have to wear one foreach also, example:

<?php

$response = file_get_contents('https://api.openload.co/1/file/info?file=6PHaY9bqRYc');

if (!$response) {
   die('Erro nos dados');
}

$data = json_decode($response, true);

if ($data['status'] != 200) {
   die('Erro HTTP:' . $data['status']);
}

foreach ($data['result'] as $id => $details) {
    echo 'id: ', $details['id'], '<br>';
    echo 'Nome: ', $details['name'], '<br>';
    echo 'Tamanho: ', $details['size'], '<br>';
    echo 'Content-Type: ', $details['content_type'], '<hr>';
}

Of the other JSON:

{"status":200,"msg":"OK","result":{"ticket":"valorticket","captcha_url":"valorcaptcha","captcha_w":160,"captcha_h":70,"wait_time":0,"valid_until":"2018-05-11 15:55:44"}}

should look like this:

<?php

//Esta em string, mas suponho que venha de uma URL
$response = '{"status":200,"msg":"OK","result":{"ticket":"valorticket","captcha_url":"valorcaptcha","captcha_w":160,"captcha_h":70,"wait_time":0,"valid_until":"2018-05-11 15:55:44"}}';

if (!$response) {
   die('Erro nos dados');
}

$data = json_decode($response, true);

if ($data['status'] != 200) {
   die('Erro HTTP:' . $data['status']);
}

$result = $data['result'];

print_r($result);

echo 'ticket: ', $result['ticket'], PHP_EOL;
echo 'URL captcha: ', $result['captcha_url'], PHP_EOL;
echo 'Largura captcha: ', $result['captcha_w'], PHP_EOL;
echo 'Largura altura: ', $result['captcha_h'], PHP_EOL;
echo 'Validade: ', $result['valid_until'], PHP_EOL;

You don’t need foreach because the result only receives "one object", while the other json received on result: a variant key object, for example "6PHaY9bqRYc": and it was still "multi-dimensional".

  • Can you help me with json in the format it is now? I tried and only error

  • @What mistake? Always be specific please, it helps to understand the problems so I can help you.

  • Illegal string offset 'ticket'

  • @Rogériosilva edited the answer, see the example for the second JSON and read the explanation.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.