使用从 cURL 返回的值进行后续调用作为授权 header
Use value returned from cURL to make a subsequent call as an Authorization header
我正在使用 cURL 调用 api,我得到了一个成功的调用,但是 api 要求我使用成功的调用来进行后续调用,特别是:使用返回的值将后续调用作为授权 header。
我不确定到底要尝试什么。我在页面的 body 中得到了正确的响应,只是通过 curl_exec($ch);但我不确定从哪里开始。
<?php
//Set Variables
$username = 'my_username';
$password = 'my_password';
$showcode = 'my_showcode';
//Set url variable with showcode as a query string
$url = "https://api.mysstaging.com/mysRest/v2/Authorize/?showCode=$showcode";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url );
//Set un/pw for basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
// grab URL and pass it to the browser
curl_exec($ch);
//Expect to store and use variable but not sure how
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . mysGUID,
));
// close cURL resource, and free up system resources
curl_close($ch);
?>
从 curl_exec($ch),我在 body 中得到了一个带有 mysGUID 的响应,我认为这是我 should/can 将其作为变量传递的地方,但我'我不确定如何将响应作为变量存储在 php 中,然后使用它进行后续调用?抱歉,我对 API 仍然很陌生,但希望我在正确的轨道上?
我能够解决这个问题。万一其他人被卡住,我采取的方法是设置 CURLOPT_RETURNTRANSFER 以便我可以利用这些值。然后我为返回的键和值创建了一个变量(我称之为 GUID)。然后我使用这个变量将输出转换为一个数组,然后允许我从返回的 GUID 中获取值,然后我将其存储在一个变量中,我可以用于后续调用。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create variable for authorization output, this gives us the GUID we need.
$authOutput = curl_exec($ch);
// Convert JSON string to Array to pull the GUID value only
$outputArray = json_decode($authOutput, true);
//create variable for myResult variable
$myResult= $outputArray[0]["myResult"];
echo "My Result = $myResult";
// close cURL resource, and free up system resources
curl_close($ch);
我正在使用 cURL 调用 api,我得到了一个成功的调用,但是 api 要求我使用成功的调用来进行后续调用,特别是:使用返回的值将后续调用作为授权 header。
我不确定到底要尝试什么。我在页面的 body 中得到了正确的响应,只是通过 curl_exec($ch);但我不确定从哪里开始。
<?php
//Set Variables
$username = 'my_username';
$password = 'my_password';
$showcode = 'my_showcode';
//Set url variable with showcode as a query string
$url = "https://api.mysstaging.com/mysRest/v2/Authorize/?showCode=$showcode";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url );
//Set un/pw for basic auth
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
// grab URL and pass it to the browser
curl_exec($ch);
//Expect to store and use variable but not sure how
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: ' . mysGUID,
));
// close cURL resource, and free up system resources
curl_close($ch);
?>
从 curl_exec($ch),我在 body 中得到了一个带有 mysGUID 的响应,我认为这是我 should/can 将其作为变量传递的地方,但我'我不确定如何将响应作为变量存储在 php 中,然后使用它进行后续调用?抱歉,我对 API 仍然很陌生,但希望我在正确的轨道上?
我能够解决这个问题。万一其他人被卡住,我采取的方法是设置 CURLOPT_RETURNTRANSFER 以便我可以利用这些值。然后我为返回的键和值创建了一个变量(我称之为 GUID)。然后我使用这个变量将输出转换为一个数组,然后允许我从返回的 GUID 中获取值,然后我将其存储在一个变量中,我可以用于后续调用。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// create variable for authorization output, this gives us the GUID we need.
$authOutput = curl_exec($ch);
// Convert JSON string to Array to pull the GUID value only
$outputArray = json_decode($authOutput, true);
//create variable for myResult variable
$myResult= $outputArray[0]["myResult"];
echo "My Result = $myResult";
// close cURL resource, and free up system resources
curl_close($ch);