json - Trouble parsing the HTTPS response for token in PHP -
i've been struggling parse https response token value need future https calls.
to parse token out of json response, i've tried following:
$doc = json_decode($response, true); token = $doc["result"]["token"];
to parse token out of xml response, i've tried following:
$json = json_encode($response); $token = json_decode($json,true)['raw_body'];
in python parse json token had following:
token = json.loads(data.text)["result"]["token"]
if copy paste console output of token later https calls works fine, thought maybe "cheat" little , capture console output , use instead, tried following:
ob_start(); echo $token; $test = ob_get_contents(); ob_end_clean(); echo $test;
however, didn't work either. next thing thought result of 'parsing' had sort of weird characters causing token fail in future https calls. tried following:
$token = preg_replace("/[^\w]+/", " ", $data['raw_body']);
and gave me interesting response: "xml version 1 0 encoding utf 8 result xmlns [url here] portal token 7eae331326c9363c9cf47ef58c1d844422dabb0 token result"
so thought split string spaces , pull out token. however, didn't work either.
in python full code looked this:
# request token data = requests.post(access_token_url, data={'apikey': apikey, "format": "json"}, auth=httpdigestauth(username, password)) # parse json token token = json.loads(data.text)["result"]["token"]
in php, have:
$data = array( 'apikey' => $this->api_key, 'format' => 'xml'); unirest\request::auth($this->username, $this->password, curlauth_digest); $response = unirest\request::post(self::build_url("auth", "get-token") // lots of comments of attempts below
any ideas? first week php , i'm totally lost.
thanks
finally got it.
i made more complicated had be.
$xml = simplexml_load_string($response->body); $token = $xml->token;
Comments
Post a Comment