Get reply JSON information

Asked

Viewed 164 times

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 am unable to extract ipport from the json page. Answer from the site:

{"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’ve tried these ways:

$proxy = $json->data->ipPort;
$proxy = $json->ipPort;

  • $json->data["0"]->ipPort;?

  • 1

    @Guilhermecostamilam, wouldn’t it $json->data[0]->ipPort, without the quotation marks?

  • @Weessmith do not remember, so I put only in comment, but think that both work

  • remains to be seen if it worked kk

  • has already got the answer?

1 answer

1

You can do it this way:

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

Exit:

ipport: 83.169.202.2:3128

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

See worked on PHP Sandbox.

Editing

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

  • Don’t forget to change json_decode($data) for json_decode($data, true), to convert to associative arrays instead of stdClass ;)

  • I had forgotten just put in the example in PHP Sandbox. @Guilhermenascimento vlw I’ll put here :D

Browser other questions tagged

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