Programing

PHP GuzzleHttp.

crosscheck 2020. 10. 11. 09:09
반응형

PHP GuzzleHttp. params로 포스트 요청을 만드는 방법은 무엇입니까?


GuzzleHttp (version 5.0)로 게시 요청을하는 방법.

다음을 시도하고 있습니다.

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

하지만 오류가 발생합니다.

PHP 치명적인 오류 : 'No method can handle the email config key'메시지와 함께 포착되지 않은 예외 'InvalidArgumentException'


이 시도

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

이후 마르코의 대답이되지 않습니다 , 당신은 다음 구문을 (jasonlfunk의 의견에 따라)를 사용해야합니다 :

$client = new \GuzzleHttp\Client();
$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 \GuzzleHttp\Client();
$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();
    }
);

디버깅에 대한 추가 정보

더 자세한 정보를 원하면 debug다음과 같은 옵션 을 사용할 수 있습니다 .

$client = new \GuzzleHttp\Client();
$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
]);

문서 는 새로운 가능성에 대해 더 명시 적입니다.


Note in Guzzle V6.0+, another source of getting the following error may be incorrect use of JSON as an array:

Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or a the "multipart" request option to send a multipart/form-data request.

Incorrect:

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

Correct:

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

Correct:

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

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

Add

openssl.cafile in php.ini file

참고URL : https://stackoverflow.com/questions/27825667/php-guzzlehttp-how-to-make-a-post-request-with-params

반응형