Get information reply JSON

0

Good afternoon guys, I need a little help.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://pubproxy.com/api/proxy?api=...');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$json = json_decode($data);
$proxy = $json->data->ipPort;
echo $proxy;

I'm not able to extract the ipport page from json. Website response:

{"data":[{"ipPort":"83.169.202.2:3128","ip":"83.169.202.2","port":"3128","country":"RU","last_checked":"2018-07-22 09:05:20","proxy_level":"anonymous","type":"http","speed":"15","support":{"https":1,"get":1,"post":1,"cookies":1,"referer":1,"user_agent":1,"google":0}}],"count":1}

I have tried these ways:

$proxy = $json->data->ipPort;
$proxy = $json->ipPort;
    
asked by anonymous 22.07.2018 / 19:34

1 answer

1

You can do this as follows:

echo 'ipPort: ' . $json["data"][0]["ipPort"];

Output:

  

ipport: 83.169.202.2:3128

The data property contains a list with the information you need, in this case we get only the first value in the index 0 which is $json["data"][0] to access the data.

See how it works in PHP Sandbox .

Issue

As mentioned by @GuilhermeNascimento, you have to pass true to the function json_decode which is the second parameter bool $assoc that transforms the object into an associative array.

    
22.07.2018 / 20:59