新的guzzle。我正在尝试使用它联系REST端点。通过curl发送请求或使用类似postman app for chrome的请求返回预期的JSON响应。使用下面的guzzle发送它会返回一个404错误,类似于我在没有标题包含的情况下点击URL时返回的错误。
为什么标题没有进入此请求?
// Get extra detail for the object
$client = new \GuzzleHttp\Client([
'base_uri' => env('OPENIDM_URL'),
'headers' => [
'Content-Type' => 'application/json',
'X-OpenIDM-Username' => env('OPENIDM_USER'),
'X-OpenIDM-Password' => env('OPENIDM_PASS'),
'Authorization' => 'Basic Og=='
]
]);
$request = new \GuzzleHttp\Psr7\Request('GET', $attributes['sourceobjectid']);
$res = $client->send($request);
我已经转储了客户端的内容并请求了对象。他们看起来如下:
Client {#181 ▼
-config: array:8 [▼
"base_uri" => Uri {#188 ▼
-scheme: "https"
-userInfo: ""
-host: "my.url.here.com"
-port: null
-path: "/openidm"
-query: ""
-fragment: ""
}
"headers" => array:5 [▼
"Content-Type" => "application/json"
"X-OpenIDM-Username" => "myuser"
"X-OpenIDM-Password" => "mypass"
"Authorization" => "Basic Og=="
"User-Agent" => "GuzzleHttp/6.2.1 curl/7.38.0 PHP/5.6.26-0+deb8u1"
]
"handler" => HandlerStack {#169 ▶}
"allow_redirects" => array:5 [▶]
"http_errors" => true
"decode_content" => true
"verify" => true
"cookies" => false
]
}
Request {#189 ▼
-method: "GET"
-requestTarget: null
-uri: Uri {#190 ▼
-scheme: ""
-userInfo: ""
-host: ""
-port: null
-path: "managed/user/eb758aab-7896-4196-8989-ba7f97a7e962"
-query: ""
-fragment: ""
}
-headers: []
-headerNames: []
-protocol: "1.1"
-stream: null
我们非常感谢任何建议。
答案 0 :(得分:1)
如果您自己构建请求对象,Guzzle将不会对其应用配置。
您必须使用从客户端调用的便捷HTTP方法(get,put等)或使用自定义中间件。
第一个更容易,第二个给你更多力量,但也有责任。