-1

I am so close given everything I have read. Really I have been working on this and trying to understand this for a couple days. Endless google searches, and php manual reading of examples. I just don't think I have hit that AHA! moment where it clicks with me.

I want to get data from js file in anther server.

<?PHP

$token = "123";
//setup the request, you can also use CURLOPT_URL
$ch = curl_init('http://www.123.com/api/channels?limit=100000');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer ' . $token
   ));

$data = curl_exec($ch);

curl_close($ch);

//$xx = json_decode($data, true);
  $someArray = json_decode($data, true);

//print_r($someArray);
 foreach ($someArray as $key => $value) {
    echo $key["id"] . ", " . $key["text"] . "<br>";
  }
?>  

** tried to parse a JSON file using PHP. But I am stuck now.

This is the content of my JSON file outbout**

 {
    "code": 100,
    "message": "get successfully",
    "all": [{
            "id": 1,
            "image": "http:\/\/www.live-plus.io\/cache\/d7\/23d79957adf9dc7d8e675f0cfa76a55d.png",
            "text": "Bein Sport",
            "order": 1,
            "is_active": 1,
            "created_at": "2017-10-22 00:03:08",
            "updated_at": "2018-05-08 16:22:53",
            "channels": [{
                    "id": 1,
                    "name": "bein News",
                    "image": "http:\/\/www.live-plus.io\/cache\/35\/7835dc385c571b09cbb8caabc07dcdb4.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 1,
                    "created_at": "2017-10-22 00:03:51",
                    "updated_at": "2018-01-18 21:22:45",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 1
                    },
                    "servers": [{
                            "id": 87,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
                            "url": "redirect\/sport1\/BeinSportNews_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:48:56",
                            "updated_at": "2018-08-17 12:54:26",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=IKcJuqTDez4txx5PLD2-eLPJx07T0UCyCgMJTWqavlg="
                        }, {
                            "id": 89,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/sport1\/BeinSportNews_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:50:23",
                            "updated_at": "2018-08-17 12:54:35",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=3mxu47vfYmQ5EwLoWNd2_Ni-j7hKWEKnc7BTgcVVgOM="
                        }, {
                            "id": 659,
                            "title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/sport1\/BeinSportNews_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 1,
                            "server_type_id": 2,
                            "created_at": "2018-03-30 14:49:50",
                            "updated_at": "2018-08-17 12:54:39",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/BeinSportNews_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HEIr-OaAmNDLE_gqvVPj8-uW6FcaG190ZLDzlNRM6mk="
                        }
                    ]
                }, {
                    "id": 2,
                    "name": "bein HD",
                    "image": "http:\/\/www.live-plus.io\/cache\/7d\/6e7dd27da3b4a9ed33dca15307ba879f.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 2,
                    "created_at": "2017-10-22 00:04:12",
                    "updated_at": "2018-01-18 21:24:14",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 2
                    },
                    "servers": [{
                            "id": 90,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647",
                            "url": "redirect\/sport1\/beINSPORTSHD_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:50:57",
                            "updated_at": "2018-08-17 12:54:43",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=zziSNmcRfuPO8du2h-u4meRkuLV40G3SBVcGTqVmYUc="
                        }, {
                            "id": 92,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/sport1\/beINSPORTSHD_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 11:52:03",
                            "updated_at": "2018-08-17 12:54:47",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=6JxLzcmNV1wdQN8ltaHaBEBfJz6Q7xwNz2UklshsLuw="
                        }, {
                            "id": 661,
                            "title": "\u062c\u0648\u062f\u0629 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/sport1\/beINSPORTSHD_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 2,
                            "server_type_id": 2,
                            "created_at": "2018-03-30 14:54:13",
                            "updated_at": "2018-08-17 12:54:52",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/sport1\/beINSPORTSHD_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=HMIy3NwWgJzMCxp8TCAO55AMhM_QT0DxChNU6nAUyIo="
                        }
                    ]
                }, {
                    "id": 3,
                    "name": "Bein Sports 1",
                    "image": "http:\/\/www.live-plus.io\/cache\/ff\/59ffb0c1f6fc5f230f7afcc750da6f12.png",
                    "is_active": 1,
                    "is_feature": 0,
                    "order": 3,
                    "created_at": "2017-10-22 00:04:34",
                    "updated_at": "2018-01-17 17:18:13",
                    "pivot": {
                        "category_id": 1,
                        "channel_id": 3
                    },
                    "servers": [{
                            "id": 1,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720)",
                            "url": "redirect\/Beinx1\/bx1_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:30:21",
                            "updated_at": "2018-08-17 18:08:16",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=85LyxJDxf9i0bU6kioFowv2I-Qon_0xPNb_M9G_kBqM="
                        }, {
                            "id": 2,
                            "title": "\u062c\u0648\u062f\u0629 \u0645\u062a\u0648\u0633\u0637\u0629 (480)",
                            "url": "redirect\/Beinx1\/bx1_480p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:33:39",
                            "updated_at": "2018-08-17 18:08:20",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_480p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=0vjltj0WknuGZ2ZH5yVdGASflpXN9WtYtZGi-PQdJ-A="
                        }, {
                            "id": 3,
                            "title": "\u062c\u0648\u062f\u0647 \u0645\u062a\u0648\u0633\u0637\u0629 (360)",
                            "url": "redirect\/Beinx1\/bx1_360p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:34:14",
                            "updated_at": "2018-08-17 18:08:24",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_360p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=S8vjztw7O8vZJfoDqmMWQ8Kd5OXgh624Fz3H1mDOdn0="
                        }, {
                            "id": 4,
                            "title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 (240)",
                            "url": "redirect\/Beinx1\/bx1_240p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:34:33",
                            "updated_at": "2018-08-17 18:08:28",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_240p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=kcIoL7nR7fGRfJ_HoA747nbjiAjphEncaj1JpIJ8_dc="
                        }, {
                            "id": 6,
                            "title": "\u062c\u0648\u062f\u0647 \u0636\u0639\u064a\u0641\u0629 - \u0645\u062e\u0635\u0635\u0647 \u0644\u0644\u0628\u0627\u0642\u0629",
                            "url": "redirect\/Beinx1\/bx1_160p",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:38:25",
                            "updated_at": "2018-08-17 18:08:32",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx1\/bx1_160p\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=nS6iGwp-kLIOMsIWygOnuamfFq13KKjJhJgJ0i2lAos="
                        }, {
                            "id": 7,
                            "title": "\u062c\u0648\u062f\u0647 \u0639\u0627\u0644\u064a\u0647 (720) \u0645\u0635\u062f\u0631 2",
                            "url": "redirect\/Beinx3\/x1_source",
                            "type": 2,
                            "is_active": 1,
                            "channel_id": 3,
                            "server_type_id": 2,
                            "created_at": "2017-10-22 00:38:47",
                            "updated_at": "2018-08-17 18:14:34",
                            "secure_url": "http:\/\/load.live-plus.tv:1935\/redirect\/Beinx3\/x1_source\/?scheme=m3u8&liveplusendtime=1535668918&liveplusstarttime=1535582518&liveplushash=1MuhTMmCh8l8EBcfMrhpRmLt8XbeVCIlwF5zDk6sbfg="

I need to get secure_url by name item

amr selim
  • 47
  • 12
  • What problem are you facing? – Muhammed Imran Hussain Aug 29 '18 at 23:27
  • Please give us the `$data` content before json-decoding. Use `echo '
    ' . print_r($data, TRUE) . '
    ';` for it.
    – PajuranCodes Aug 29 '18 at 23:28
  • Muhammed Imran Hussain i cant prest data from js file – amr selim Aug 29 '18 at 23:30
  • There are multiple instances of secure_url. Which one(s) are you trying to get? Can you show an example of the output you need based on that example input? – Don't Panic Aug 29 '18 at 23:31
  • ineed to get chanel url by name from outbut js file – amr selim Aug 29 '18 at 23:33
  • @dakis i edit the code in post try it – amr selim Aug 29 '18 at 23:36
  • @Don'tPanic how can i parse outbout and get secure_url for id or text i reqst it – amr selim Aug 29 '18 at 23:49
  • @amrselim We can help you but you need to edit your question to express EXACTLY what your desired output is -- based on your sample input. It is also important that you provide a valid input json string. It doesn't need to have 10,000 pieces of data in it, but it should contain enough data to be accurate for your purposes. Please don't truncate the end of the string to "cut it down"; rather you should remove "middle portions of data" while maintaining validity so that we can copy-paste your data and we can test our solutions before posting. – mickmackusa Aug 30 '18 at 01:20
  • Not only is this question Unclear, it can just as easily be closed with: https://stackoverflow.com/q/29308898/2943403 – mickmackusa Aug 30 '18 at 03:02

1 Answers1

1

Theoretically, you should need three criterias, in order to find a specific secure_url:

  • The id of an element in the $decodedData['all'] array ($searchedAllId);
  • The id of a channel in the channels list of the above array element ($searchedChannelId);
  • The id of a server in the servers list of the above channel element ($searchedServerId).

If you have other requirement(s) or perspective, then tell us in (more) detail, so that we can give you the answer suited to the new situation(s).

I think you can easily follow the steps I used in the code. I didn't apply any validation on the array elements, in order to keep it simple.

//...

$data = curl_exec($ch);

curl_close($ch);

$decodedData = json_decode($data, true);

//=========================================================
// This line is just for testing: display the decoded data.
//echo '<pre>' . print_r($decodedData, TRUE) . '</pre>';
//=========================================================
//
// Criterias to search for in the decoded data.
$searchedAllId = 1;
$searchedChannelId = 1;
$searchedServerId = 659;

// Holds the found secure url.
$foundSecureUrl = '';

// Search for the secure url based on the above criterias.
foreach ($decodedData['all'] as $allKey => $allItem) {
    if ($allItem['id'] === $searchedAllId) {
        foreach ($allItem['channels'] as $channelKey => $channelItem) {
            if ($channelItem['id'] === $searchedChannelId) {
                foreach ($channelItem['servers'] as $serverKey => $serverItem) {
                    if ($serverItem['id'] === $searchedServerId) {
                        $foundSecureUrl = $serverItem['secure_url'];

                        /*
                         * Secure url is now found.
                         * So break the further execution of all THREE foreach loops.
                         */
                        break(3);
                    }
                }
            }
        }
    }
}

// Display the found secure url.
echo 'Found secure url:<br/><br/>' . $foundSecureUrl;

The $decodedData array looks like this, after I shorted it and changed the sensible data in it:

Array
(
    [code] => 100
    [message] => get successfully
    [all] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [image] => some-image.png
                    [text] => Bein Sport
                    [order] => 1
                    [is_active] => 1
                    [created_at] => 2017-10-22 00:03:08
                    [updated_at] => 2018-05-08 16:22:53
                    [channels] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 1
                                    [name] => bein News
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 1
                                    [created_at] => 2017-10-22 00:03:51
                                    [updated_at] => 2018-01-18 21:22:45
                                    [pivot] => Array
                                        (
                                            [category_id] => 1
                                            [channel_id] => 1
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 87
                                                    [title] => جوده عاليه
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 1
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:48:56
                                                    [updated_at] => 2018-08-17 12:54:26
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 89
                                                    [title] => جوده متوسطة (360)
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 1
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:50:23
                                                    [updated_at] => 2018-08-17 12:54:35
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 2
                                    [name] => bein HD
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 2
                                    [created_at] => 2017-10-22 00:04:12
                                    [updated_at] => 2018-01-18 21:24:14
                                    [pivot] => Array
                                        (
                                            [category_id] => 1
                                            [channel_id] => 2
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 90
                                                    [title] => جوده عاليه
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 2
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:50:57
                                                    [updated_at] => 2018-08-17 12:54:43
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 92
                                                    [title] => جوده متوسطة (360)
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 2
                                                    [server_type_id] => 2
                                                    [created_at] => 2017-10-22 11:52:03
                                                    [updated_at] => 2018-08-17 12:54:47
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            ...

                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [image] => some-image.png
                    [text] => bein
                    [order] => 2
                    [is_active] => 1
                    [created_at] => 2017-10-22 12:53:17
                    [updated_at] => 2018-05-08 16:22:58
                    [channels] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 536
                                    [name] => BBC EARTH
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 0
                                    [created_at] => 2018-08-17 15:53:06
                                    [updated_at] => 2018-08-17 15:53:06
                                    [pivot] => Array
                                        (
                                            [category_id] => 2
                                            [channel_id] => 536
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1136
                                                    [title] => جودة عالية
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 536
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:01:26
                                                    [updated_at] => 2018-08-17 16:01:26
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 1137
                                                    [title] => جوده متوسطة
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 536
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:01:58
                                                    [updated_at] => 2018-08-17 16:01:58
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 535
                                    [name] => BeIN Dlife
                                    [image] => some-image.png
                                    [is_active] => 1
                                    [is_feature] => 0
                                    [order] => 0
                                    [created_at] => 2018-08-17 15:52:15
                                    [updated_at] => 2018-08-17 15:52:15
                                    [pivot] => Array
                                        (
                                            [category_id] => 2
                                            [channel_id] => 535
                                        )

                                    [servers] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [id] => 1133
                                                    [title] => جودة عالية
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 535
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:00:13
                                                    [updated_at] => 2018-08-17 16:00:13
                                                    [secure_url] => some-secure-url
                                                )

                                            [1] => Array
                                                (
                                                    [id] => 1134
                                                    [title] => جودة متوسطة
                                                    [url] => some-url
                                                    [type] => 2
                                                    [is_active] => 1
                                                    [channel_id] => 535
                                                    [server_type_id] => 2
                                                    [created_at] => 2018-08-17 16:00:35
                                                    [updated_at] => 2018-08-17 16:00:35
                                                    [secure_url] => some-secure-url
                                                )

                                            ...

                                        )

                                )

                            ...
                        )

                )

            ...

        )

)
PajuranCodes
  • 303
  • 3
  • 12
  • 43
  • thanks @dakis it works perfect – amr selim Aug 30 '18 at 09:21
  • @amrselim I am glad to hear that. You are welcome. P.S: You should replace the sensitive data in your codes with something else, or just delete it from them, because everyone can see and use it. Take my answer as model, if you wish. Good luck. – PajuranCodes Aug 30 '18 at 11:01