新手入门。我正在尝试使用它联系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任何建议都将不胜感激。
发布于 2016-11-09 18:49:56
如果你自己构造请求对象,Guzzle将不会对其应用配置。
您要么必须使用从客户端调用的方便的HTTP方法(get、put等),要么使用自定义的中间件。
第一个更容易,第二个给你更多的权力,但也有责任。
https://stackoverflow.com/questions/40221643
复制相似问题