我有一些代码要求从Jive的API中获取数据。我在MAMP上运行此代码,它允许我运行PHP 5.6.10或PHP 7.0.0。使用PHP5,我得到了成功的回复。使用PHP 7,我获得401 Unauthorized。
相关功能在这里:
protected function sendRequest($method, $url, $auth = null) {
global $CFG;
$options = func_num_args() === 4 ? func_get_arg(3) : array();
$http = array(
'max_redirects' => 0,
'request_fulluri' => 1,
'ignore_errors' => true,
'method' => $method,
'header' => array()
);
if (!is_null($auth)) {
array_push($http['header'], 'Authorization: ' . $auth);
}
if (($method === 'PUT' || $method === 'POST') && isset($options['content'])) {
$http['content'] = $options['content'];
array_push($http['header'], 'Content-length: ' . strlen($options['content']));
array_push($http['header'], 'Content-Type: application/json');
}
var_dump($http);echo('<hr/>');
$context = stream_context_create(array( 'http' => $http ));
var_dump(stream_context_get_options($context));echo('<hr/>');
$fp = fopen($url, 'rb', false, $context);
if (! $fp) {
throw new \Exception('Request failed: $php_errormsg');
}
$metadata = stream_get_meta_data($fp);
$content = stream_get_contents($fp);
$responseCode = (int)explode(' ', $metadata['wrapper_data'][0])[1];
fclose($fp);
return array (
'metadata' => $metadata,
'content' => $content,
'status' => $responseCode
);
}
var_dump调用与两个PHP版本产生相同的结果。我得到的回应是:
{
"metadata": {
"wrapper_data": [
"HTTP/1.0 401 Unauthorized",
"Server: Apache",
"X-Jive-Request-Id: 6c433c20-688a-11e6-b332-005056a4250c",
"X-Jive-Flow-Id: 6c433c21-688a-11e6-b332-005056a4250c",
"X-Frame-Options: SAMEORIGIN",
"Expires: Mon, 22 Aug 2016 17:04:01 GMT",
"Cache-Control: no-store, no-cache, must-revalidate, private, max-age=0",
"X-JSL: D=1754 t=1471885441249342",
"Content-Type: text/plain",
"Date: Mon, 22 Aug 2016 17:04:01 GMT",
"Connection: close",
"Set-Cookie: jive.login.ts=1471885441250; Path=/; Secure; HttpOnly;HttpOnly",
"Set-Cookie: X-JCAPI-Token=pTVEn2P4; Path=/; Secure; HttpOnly",
"Set-Cookie: BIGipServerpool_sandbox.jiveon.com=25472522.20480.0000; path=/"
],
"wrapper_type": "http",
"stream_type": "tcp_socket/ssl",
"mode": "rb",
"unread_bytes": 0,
"seekable": false,
"uri": "https://sandbox.jiveon.com/api/core/v3/activities?after=2016-08-22T17:01:14%2b0000&count=500",
"crypto": {
"protocol": "TLSv1",
"cipher_name": "ECDHE-RSA-AES256-SHA",
"cipher_bits": 256,
"cipher_version": "TLSv1/SSLv3"
},
"timed_out": false,
"blocked": true,
"eof": false
},
"content": "",
"status": 401,
"success": false
}
使用https://requestb.in我可以看到PHP7版本不包含授权标题
PHP 5.6.10和PHP 7之间发生了什么变化导致了这种情况?我如何解决它?
编辑:删除一些红鲱鱼文本并添加请求文件结果。
答案 0 :(得分:0)
以下作品:
protected function sendRequest($method, $url, $auth = null) {
global $CFG;
$options = func_num_args() === 4 ? func_get_arg(3) : array();
$http = array(
'max_redirects' => 0,
'request_fulluri' => 1,
'ignore_errors' => true,
'method' => $method
);
$headers = array();
if (!is_null($auth)) {
array_push($headers, 'Authorization: ' . $auth);
}
if (($method === 'PUT' || $method === 'POST') && isset($options['content'])) {
$http['content'] = $options['content'];
array_push($headers, 'Content-length: ' . strlen($options['content']));
array_push($headers, 'Content-Type: application/json');
}
$http['header'] = $headers;
$context = stream_context_create(array( 'http' => $http ));
var_dump(stream_context_get_options($context));echo('<hr/>');
$fp = fopen($url, 'rb', false, $context);
if (! $fp) {
throw new \Exception('Request failed: $php_errormsg');
}
$metadata = stream_get_meta_data($fp);
$content = stream_get_contents($fp);
$responseCode = (int)explode(' ', $metadata['wrapper_data'][0])[1];
fclose($fp);
return array (
'metadata' => $metadata,
'content' => $content,
'status' => $responseCode
);
}
我现在将所有标头存储在数组变量中并将header属性设置为该属性。
我不是百分百肯定为什么,但我认为它与变量引用有关。在代码的一次迭代中,$ http授权属性被var_dump标记为&符号,而fopen正在忽略它。删除该&符号的另一个迭代工作。