Php 如何通过使用auth和body raw从狂饮中获得响应?
我试着在邮递员身上这样做:Php 如何通过使用auth和body raw从狂饮中获得响应?,php,laravel,api,laravel-5.3,guzzle,Php,Laravel,Api,Laravel 5.3,Guzzle,我试着在邮递员身上这样做: public function testApi() { $requestContent = [ 'headers' => [ 'Accept' => 'application/json', 'Content-Type' => 'application/json' ], 'json' => [ 'auth' =>
public function testApi()
{
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
'ids' => ["1"]
]
];
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
$response = json_decode($apiRequest->getBody());
dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}
我只需输入密码。然后我点击按钮更新请求
这样的观点:
public function testApi()
{
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
'ids' => ["1"]
]
];
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
$response = json_decode($apiRequest->getBody());
dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}
这是标题:
这是尸体。我选择原始数据和输入数据,如下所示:
public function testApi()
{
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
'ids' => ["1"]
]
];
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
$response = json_decode($apiRequest->getBody());
dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}
然后我点击发送按钮,它可以得到响应
但是当我试着像这样使用guzzle 6时:
public function testApi()
{
$requestContent = [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json'
],
'json' => [
'auth' => ['', 'b0a619c152031d6ec735dabd2c6a7bf3f5faaedd'],
'ids' => ["1"]
]
];
try {
$client = new GuzzleHttpClient();
$apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
$response = json_decode($apiRequest->getBody());
dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}
结果是空的
如何解决此问题?请参见《邮递员》中的内容,您在“邮件头”选项卡中正确指定了
授权
。所以当你使用Guzzle时应该是一样的,把它放在标题中:
public function testApi()
{
$requestContent = [
'auth' => ['username', 'password']
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => [
'ids' => ["1"]
]
];
try {
$client = new GuzzleHttpClient;
$apiRequest = $client->request('POST', 'https://myshop/api/order', $requestContent);
$response = json_decode($apiRequest->getBody());
dd($response);
} catch (RequestException $re) {
// For handling exception.
}
}
看来我的服务器有问题了。所以我不能尝试。后来我试了一下,效果很好。我会接受你的回答好的,让我知道。我编辑了我的答案,因为我忘记了
'Content-Type'=>'application/json'
行末尾的逗号。我试过了。但它不起作用。它重定向到页面登录。这似乎不太可信。我看到在你的编码中,没有身份验证。但在我展示的邮递员中,它似乎是这样的:“授权”=>“基本b0a619c152031d6ec735dabd2c6a7bf3f5faaedd”
<代码>b0a619c152031d6ec735dabd2c6a7bf3f5faaedd并且输入了密码,它不应该是我输入的相同密码:2AE694A084650944EA04AB21506E8E740FB55B6EB
当我在邮递员上单击更新请求时,标题中的结果是:基本OJHZTY5NGEWODQ2NTA55NGVHMDRHYXNTA2ZLHLNZMI1NWI2WI=/code>