如何使用标头

时间:2018-02-08 21:36:47

标签: php laravel api http-post postman

我需要将API集成到我的应用中。 文档说:

HTTP POST     以下是HTTP POST请求和响应示例。显示的占位符需要替换为实际值。

POST /partnerhubwebservice.asmx/Authorise HTTP/1.1
Host: stage.example.co.uk
Content-Type: application/x-www-form-urlencoded
Content-Length: length

username=string&password=string

并且回复是:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns="http://webservice.example.co.uk/">
  <Token>string</Token>
</AuthorisationResult>

阅读文档我在Laravel Controller中创建了这个函数:

public function testRld() {

    $client = new GuzzleHttp\Client();
try {
    $res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
            ],
    'x-www-form-urlencoded' => [
        'Username' => 'MMM_bookings',
        'Password' => 'passwordaaaa',
]
            ]);

return $res;

}

catch (GuzzleHttp\Exception\ClientException $e) {
        $response = $e->getResponse();
        $result =  json_decode($response->getBody()->getContents());

    return response()->json(['data' => $result]);

    }

}

但我收到了一条消息:

  

RequestException.php第111行中的ServerException:服务器错误:POST http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise   导致500 Internal Server Error响应:缺少参数:   用户名。

当我在POSTMAN应用程序中尝试这一切时,一切都很好,我得到了响应ike:

<?xml version="1.0" encoding="utf-8"?>
<AuthorisationResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservice.example.co.uk/">
    <RequestSuccessful>true</RequestSuccessful>
    <ErrorMessage />
    <Token>27d67d31-999-44e0-9851-d6f427fd2181</Token>
</AuthorisationResult>

请帮我解决这个问题?我的代码出了什么问题?为什么我收到错误,POSTMAN请求正常...

2 个答案:

答案 0 :(得分:2)

尝试在bodyjson内部发送用户名和密码,而不是application/x-www-form-urlencoded,如下所示:

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'json' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

$res = $client->post('http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'body' => [
        'username' => 'MMM_bookings',
        'password' => 'passwordaaaa',
    ]
]);

答案 1 :(得分:0)

使用form_params

$client->request('POST', 'http://stage.example.co.uk/partnerhubwebservice.asmx/Authorise', [
    'headers' => [
        'Content-Type' => 'application/x-www-form-urlencoded',
    ],
    'form_params' => [
        'username' => 'MMM_bookings',
        'password' => 'password'
    ]
]);
  

PSR-7标准指定HTTP消息的接口,包括请求和响应。如果您想获取PSR-7请求的实例而不是Laravel请求,您首先需要安装一些库。 Laravel使用Symfony HTTP消息桥组件将典型的Laravel请求和响应转换为PSR-7兼容的实现:

安装必要的依赖项:

composer require symfony/psr-http-message-bridge
composer require zendframework/zend-diactoros