首页 > 解决方案 > 如何分解和重用 curlopt 响应?

问题描述

我一直在努力curlopt回复很长一段时间,希望大家能给我一些指导。

如果您请访问以下页面:https ://niarthgin.com/api/result.php

这将使用 生成一个简单的响应curlopt。这是我用来获得这些结果的代码:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://www.bungie.net/Platform/Destiny2/SearchDestinyPlayer/2/Niarthgin/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
    "postman-token: xxxx",
    "x-api-key: xxxx"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
      echo "cURL Error #:" . $err;
} else {
    echo $response;

}
?>

我想打破这种反应,但我不知道该怎么做。例如,在该响应中,您有MembershipType一个displayName等。如何单独解析响应?就像我知道我可以解析响应一样,json.parse()但这只会解析给定的字符串。我希望能够分段解析响应..

谢谢阅读!:)

标签: phpjsonparsingcurlresponse

解决方案


推荐阅读