好吧,我在Guzzle上玩耍,似乎我无法帖子上班。它在卷曲CLI上起作用,但在Laravel Guzzle中不起作用。我做错了吗?
这是我的卷发(工作正常)
curl -i -d 'locationID=2&firstname=John&middlename=Dee&lastname=Doe&sourceID=2&typeID=2&email=johndoe@yahoo.com&emailtypeID=2&phone=1234567890&phonetypeID=2' http://api.company.com/v1/members
HTTP/1.1 200 OK
Date: Thu, 07 May 2015 14:15:25 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.5
Cache-Control: no-cache
Set-Cookie: laravel_session=eyJpdi;
expires=Thu, 07-May-2015 16:15:26 GMT;
Max-Age=7200;
path=/; httponly
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Content-Length: 59
Content-Type: application/json
这是我的控制器
use GuzzleHttpClient;
public function postForm(Request $request)
{
$client = new Client();
$response = $client->post('http://api.company.com/v1/members',
array(
'body' => array(
'firstname' => $firstname,
'middlename' => $middlename,
'lastname' => $lastname,
'locationID' => $locationID,
'phonetypeID' => $phonetypeID,
'phone' => $phone,
'emailtypeID' => $emailtypeID,
'email' => $email,
'sourceID' => $sourceID,
'typeID' => $typeID
)
)
);
$body = $response->send();
$content = view('member.create_result')->with('member',json_decode($body, TRUE));
return Admin::view($content, 'Member Added');
}
错误我得到
Client error response [url] http://api.company.com/v1/members [status code] 404 [reason phrase] Not Found
尝试此
$body = [
'firstname' => $firstname,
'middlename' => $middlename,
'lastname' => $lastname,
'locationID' => $locationID,
'phonetypeID' => $phonetypeID,
'phone' => $phone,
'emailtypeID' => $emailtypeID,
'email' => $email,
'sourceID' => $sourceID,
'typeID' => $typeID
]
$options = [
'body' => $body
];
$request = $client->createRequest('POST', 'http://api.company.com/v1/members', $options);
$response = $client->send($request);
我遇到了相同的问题(在Postman中工作并使用卷曲和guzzle的请求)。事实证明,问题是缺少CORS标题。我使用此软件包将它们添加到我的路线中-https://github.com/barryvdh/laravel-cors一切正常:)
也许这会帮助某人,因为我花了太多时间在这方面花了很多时间。