Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我收到一个json帖子的身份验证错误_Php_Json_Jwt - Fatal编程技术网

Php 我收到一个json帖子的身份验证错误

Php 我收到一个json帖子的身份验证错误,php,json,jwt,Php,Json,Jwt,我正在尝试验证,请求的结果返回错误 请求模型 方法:Post,端点:/api/身份验证,标题变量: [{“key”:“Content Type”,“value”:“application/json”,“enabled”:true}], 正文参数:用户名:字符串,密码:字符串, authenticationType:字符串 样本请求 POST /api/authenticate Host: mpop-sit.hepsiburada.com Content-Type: application/js

我正在尝试验证,请求的结果返回错误

请求模型

方法:Post,端点:/api/身份验证,标题变量: [{“key”:“Content Type”,“value”:“application/json”,“enabled”:true}], 正文参数:用户名:字符串,密码:字符串, authenticationType:字符串

样本请求

POST /api/authenticate 
Host: mpop-sit.hepsiburada.com
Content-Type: application/json
{
   "username": "xyz_dev",
   "password": "XYZ_dev123!",
   "authenticationType": "INTEGRATOR"
}
$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array(
    'Content-Type: application/json',
    'Authorization: Bearer '. base64_encode('xyz_dev:XYZ_dev123!:INTEGRATOR'),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);
我发送的请求

POST /api/authenticate 
Host: mpop-sit.hepsiburada.com
Content-Type: application/json
{
   "username": "xyz_dev",
   "password": "XYZ_dev123!",
   "authenticationType": "INTEGRATOR"
}
$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array(
    'Content-Type: application/json',
    'Authorization: Bearer '. base64_encode('xyz_dev:XYZ_dev123!:INTEGRATOR'),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);
这是返回的查询结果和我收到的错误。 你认为我可能在哪里犯错

数组([时间戳]=>2020-02-07T09:01:47.426+0000[状态]=>500 [错误]=>内部服务器错误[异常]=> io.jsonwebtoken.MalformedJwtException[消息]=>JWT字符串必须 仅包含2个句点字符。找到:0[路径]=> //api/认证)


是否需要此端点的身份验证标头

因为示例请求希望您发送请求正文中的参数,如下所示:

$post=[
'username'=>'xyz_dev',
“密码”=>“XYZ_dev123!”,
'authenticationType'=>'INTEGRATOR'
];
$url='1https://mpop-sit.hepsiburada.com//api/authenticate';
$ch=curl\u init($url);
$header=array('Content-Type:application/json');
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
$result=curl\u exec($ch);
$return=json_decode($result,true);
打印(返回);

因此,无需使用
身份验证:承载…
头,它的创建方式也会有所不同。

这应该可以正常工作!我得到了拒绝访问错误,所以,这意味着代码工作正常

注意:您可能需要将http\u build\u query($fields)更改为
$fields

$url = "https://mpop-sit.hepsiburada.com/api/authenticate/";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$fields = array(
   "username" => "xyz_dev",
   "password" => "XYZ_dev123!"
);

$header = array(
    'Content-Type: application/json',
    'Authorization' => 'Bearer ' . $token,
);

//open curl connection
$ch = curl_init();

//set the url, fields, vars
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // SSL false if not required
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); //False if not required
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute fields
$result = curl_exec($ch);
//return result echo $result; if you need
echo curl_error($ch);
//close curl connection
curl_close($ch);
请让我知道它是否有效

更新:如果您想使用ssl,这将使您免受黑客攻击


按照此答案中的步骤激活ssl:

此处有两个斜杠,因此它不起作用:

$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';

这回答了你的问题吗?没有,我检查过,但没有得到任何结果更改
https://mpop-sit.hepsiburada.com/api/authenticate
https://mpop-sit.hepsiburada.com/api/authenticate/
谢谢,但我得到了同样的错误:(@Mahlika将此添加到代码末尾
echo curl\u error($ch);curl\u close($ch)
第一个将给出正确的错误,第二个将关闭curl连接。我得到SSL证书问题:无法使用您的代码和我上面评论中的第二个url在localhost上获得本地颁发者证书。这是正确的url:
https://mpop-sit.hepsiburada.com/api/authenticate/
我想,请参阅sta中有关ssl的这篇文章声明,身份验证应该是承载的,而不是基本的!表示兴趣通常身份验证凭据(如
承载的
所使用的)包含在HTTP头中,而不是正文中。@MikeRobinson是的,但Mahlika给出的示例请求看起来根本不需要身份验证头。总之,用户名、pas和剑,和authencationType不会构成JWT令牌(请参阅)请不要关闭SSL验证。它会使您的代码易受攻击。