A
A
Aibek Zhaksybaev2018-04-23 13:35:40
PHP
Aibek Zhaksybaev, 2018-04-23 13:35:40

How to output specific data in json array?

There is an application that works with instagram without api, where in the search via curl data is taken from the instagram search
, here is the data
5addb66be3539303282429.jpeg
. Curl code:

public function fetchData($url){
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_TIMEOUT, 20);
            $result = json_decode(curl_exec($ch), true);
            curl_close($ch);
            return $result;
        } catch(Exception $e) {
            return $e->getMessage();
        }
    }

Search code:
public function getSearchProfiles($ignore=null) {
        $url = 'https://www.instagram.com/web/search/topsearch/?context=blended&query='.str_replace(' ', '.', $this->query).'&rank_token=';
        $result = $this->fetchData($url);
        $this->data['profiles'] = $result;        

        if($ignore != null) {
            foreach($this->data['profiles'] as $subKey => $subArray){
                if($subArray['username'] == $ignore){
                    unset($this->data['profiles'][$subKey]);
                }
            }
        }
    }

But when outputting data, the following error occurs Warning: Illegal string offset. What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yan-s, 2018-04-23
@Yan-s

Everything tells you correctly, if in $this->data['profiles'] what you have in the screenshot, then there is no "username" key in this array. There are several nested arrays, and to get to "username" you need to iterate through them all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question