这是我的卷曲(工作正常)
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 GuzzleHttp\Client;
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
答案 0 :(得分:1)
试试这个
$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);
答案 1 :(得分:0)
我遇到了同样的问题(请求在邮递员工作并且使用CURL和Guzzle失败)。原来问题是缺少CORS标头。我使用这个包将它们添加到我的路线中 - https://github.com/barryvdh/laravel-cors 一切正常:)
也许这会对某些人有所帮助,因为我花了太多时间来讨论这个问题。