事实上,据我所知,hook.io支持curl。但是,如果您想尝试其他替代方案,可以使用
我正试图在hook.io上创建一个服务,以便从另一个API加载令牌。
public function loadToken()
{
$computedHash = base64_encode(hash_hmac ( 'md5' , $this->authServiceUrl , $this->password, true ));
$authorization = 'Authorization: Bearer '.$this->username.':'.$computedHash;
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_URL, $this->authServiceUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$obj = json_decode($result);
$info = curl_getinfo($curl);
curl_close($curl);
if($info['http_code'] != '200')
{
// print error from the server
echo($obj);
return NULL;
}
return $obj;
}
但事实证明hook.io在PHP中不支持cUrl。我知道它可以直接用php完成,但我不知道怎么做。
编辑:我现在使用了fileget_contents,它现在给出了一些其他错误。
$username = '';
$password = '';
$authServiceUrl = '';
$computedHash = base64_encode(hash_hmac ( 'md5' , $authServiceUrl , $password, true ));
$authorization = 'Authorization: Bearer '.$username.':'.$computedHash;
// Create map with request parameters
// Build Http query using params
// Create Http context details
$contextData = array (
'method' => 'POST',
'header' => "Content-Type: application/json". $authorization ,
);
// Create context resource for our request
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$result = file_get_contents (
$authServiceUrl, // page url
false,
$context);
我在下方的注释中添加了错误
file_get_contents
。它支持自定义上下文和参数。
$opts = [
'http' => [
'method' => 'POST',
'headers' => ['Authorization' => 'Bearer ' . $this->username . ':' . $computedHash]
]
];
$context = stream_context_create($opts);
$file = file_get_contents('http://www.hook.io/example/', false, $context);