在CURLOPT_HTTPHEADER上分配php变量

时间:2014-10-30 02:15:34

标签: php curl openam

我试图测试OpenAM REST API的身份验证和注销功能。但是,我遇到了一个问题。

身份验证API方法正常。但是,我似乎无法处理注销API方法。我认为它与在CURLOPT_HTTPHEADER中放入令牌ID键有关。我在php变量中放了一个字符串,并尝试将其放在CURLOPT_HTTPHEADER中。

它适用于身份验证方法。但是,不适用于退出:

=============================================== ================================================== =

$cg = curl_init("http://openam.example.com:8080/openam/json/authenticate");

curl_setopt($cg, CURLOPT_CUSTOMREQUEST, "POST");
// curl_setopt($cg, CURLOPT_HTTPHEADER, array('X-OpenAM-Username: amuser', 'X-OpenAM-Password: ampwd', 'Content-Type: application/json'));
$ampwd = "ampwd";

curl_setopt($cg, CURLOPT_HTTPHEADER, array('X-OpenAM-Username: amuser', 'X-OpenAM-Password: ' . "ampwd", 'Content-Type: application/json'));
curl_setopt($cg, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cg, CURLOPT_HEADER, true);

$output = curl_exec($cg);
if(!curl_errno($cg))
{
    $info = curl_getinfo($cg);

    echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
    echo "fail";

// curl_close($cg);
echo "<pre>";
print_r($output);
echo "</pre>";

$tokenId = preg_match_all('/\{(.*?)\}/', $output, $match);
echo "<pre>";
print_r($match);
echo "</pre>";

$tokenId = preg_match_all('/\"(.*?)\"/', $match[1][0], $anothermatch);
echo "<pre>";
    $tokenIdTitle = $anothermatch[1][0];
    $tokenIdValue = '"' . $anothermatch[1][1] . '"';
    $successUrlTitle = $anothermatch[1][2];
    $successUrlValue = $anothermatch[1][3];
echo "</pre>";

$ch = curl_init("http://openam.example.com:8080/openam/json/sessions/?_action=logout");

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", 'iplanetDirectoryPro: ' . $tokenIdValue));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);

$output = curl_exec($ch);
if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);

    echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
    echo "fail";

echo "<pre>";
print_r($output);
echo "</pre>";

curl_close($ch);
?>

这是我得到的结果:


Took 0.032 seconds to send a request to http://openam.example.com:8080/openam/json/authenticate
HTTP/1.1 200 OK
Set-Cookie: amlbcookie=01; Domain=.ufcfit.com; Path=/
Date: Thu, 30 Oct 2014 01:59:24 GMT
Accept-Ranges: bytes
Server: Restlet-Framework/2.1.7
Vary: Accept-Charset, Accept-Encoding, Accept-Language, Accept
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked

{"tokenId":"AQIC5wM2LY4Sfcyv1r3DrnDThcw3mMH7GbcKKb0E8UIfGEo.*AAJTSQACMDEAAlNLABQtNTAxMDIyNTMxNjAzNDE0ODkwMg..*","successUrl":"/openam/console"}
Array
(
    [0] => Array
        (
            [0] => {"tokenId":"AQIC5wM2LY4Sfcyv1r3DrnDThcw3mMH7GbcKKb0E8UIfGEo.*AAJTSQACMDEAAlNLABQtNTAxMDIyNTMxNjAzNDE0ODkwMg..*","successUrl":"/openam/console"}
        )

    [1] => Array
        (
            [0] => "tokenId":"AQIC5wM2LY4Sfcyv1r3DrnDThcw3mMH7GbcKKb0E8UIfGEo.*AAJTSQACMDEAAlNLABQtNTAxMDIyNTMxNjAzNDE0ODkwMg..*","successUrl":"/openam/console"
        )

)
Took 0.015 seconds to send a request to http://openam.example.com:8080/openam/json/sessions/?_action=logout
HTTP/1.1 401 Unauthorized
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=ISO-8859-1
Content-Length: 69
Date: Thu, 30 Oct 2014 01:59:24 GMT

{ "code": 401, "reason": "Unauthorized", "message": "Access Denied" }

0 个答案:

没有答案