PHP GuzzleHttp.

GuzzleHttp(버전 5.0)를 사용한 투고 요구 방법.

다음을 수행하려고 합니다.

$client = new GuzzleHttpClient(); $client->post(
'http://www.example.com/user/create',
array(
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword'
) ); 

하지만 다음과 같은 오류가 발생합니다.

PHP 치명적 오류:수집되지 않은 예외 ‘InvalidArgument’예외’ 메시지와 함께 ‘No method can could hand email config key’ 메시지가 표시됨



질문에 대한 답변



Marco의 답변은 권장되지 않으므로 다음 구문을 사용해야 합니다(jasonlfunk의 코멘트에 따라).

$client = new GuzzleHttpClient(); $response = $client->request('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
] ]); 

POST 파일에 의한 요구

$response = $client->request('POST', 'http://www.example.com/files/post', [
'multipart' => [
[
 'name'
=> 'file_name',
 'contents' => fopen('/path/to/file', 'r')
],
[
 'name'
=> 'csv_header',
 'contents' => 'First Name, Last Name, Username',
 'filename' => 'csv_header.csv'
]
] ]); 

REST 동사와 매개 변수 사용

// PUT $client->put('http://www.example.com/user/4', [
'body' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
'timeout' => 5 ]);
// DELETE $client->delete('http://www.example.com/user'); 

비동기 POST 데이터

장시간 서버 조작에 편리합니다.

$client = new GuzzleHttpClient(); $promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
] ]); $promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "n";
},
function (RequestException $e) {
echo $e->getMessage() . "n";
echo $e->getRequest()->getMethod();
} ); 

헤더 설정

설명서에 따르면 헤더를 다음과 같이 설정할 수 있습니다.

// Set various headers on a request $client->request('GET', '/get', [
'headers' => [
'User-Agent' => 'testing/1.0',
'Accept'
=> 'application/json',
'X-Foo'
=> ['Bar', 'Baz']
] ]); 

디버깅에 대한 상세 정보

자세한 정보가 필요한 경우 다음을 사용할 수 있습니다.debug다음과 같은 옵션:

$client = new GuzzleHttpClient(); $response = $client->request('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
// If you want more informations during request
'debug' => true ]); 

문서화는 새로운 가능성에 대한 더 많은 설명입니다.




이거 드셔보세요

$client = new GuzzleHttpClient(); $client->post(
'http://www.example.com/user/create',
array(
'form_params' => array(
 'email' => 'test@gmail.com',
 'name' => 'Test user',
 'password' => 'testpassword'
)
) ); 



Guzle V6.0+에서는, 다음의 에러가 발생하는 또 다른 원인은, 어레이로서 JSON 를 잘못 사용하는 것일 가능성이 있습니다.

POST 요청을 보내기 위한 어레이로 “본문” 요청 옵션을 전달하는 것은 권장되지 않습니다.응용 프로그램/x-www-form-urlencoded 요청을 보내려면 “form_params” 요청 옵션을, 멀티파트/폼 데이터 요청을 보내려면 “multipart” 요청 옵션을 사용하십시오.

틀렸다:

$response = $client->post('http://example.com/api', [
'body' => [
'name' => 'Example name',
] ]) 

정답:

$response = $client->post('http://example.com/api', [
'json' => [
'name' => 'Example name',
] ]) 

정답:

$response = $client->post('http://example.com/api', [
'headers' => ['Content-Type' => 'application/json'],
'body' => json_encode([
'name' => 'Example name',
]) ]) 



$client = new GuzzleHttpClient(); $request = $client->post('http://demo.website.com/api', [
'body' => json_encode($dataArray) ]); $response = $request->getBody(); 

더하다

openssl.cafilephp.ini파일




GuzzleHttpClient를 사용하여 이미지를 직접 업로드하기 위한 Easy Call Multipart API를 사용할 수 있습니다.

use GuzzleHttpClient; use GuzzleHttpPsr7Utils; use File;
$filename = $req->file('file1')->getClientOriginalName();
$getfilePath
= $req->file('file1')->getRealPath();
$client = new Client(); $response = $client->request('POST', 'http://127.0.0.1:8045/api/uploadImages', [
'multipart' => [
[
 'name'
=> 'image',
 'contents' => fopen($getfilePath, 'r')
],
// 'headers'
=> [
//
'Content-Type' => '<Content-type header>'
//
]
 ] ]); echo $response->getStatusCode(); $bodyresponcs = $response->getBody(); $result = json_decode($bodyresponcs); print_r($result->status);