Guzzle;使用基础 URL、Headers 和 POST 设置客户端
Guzzle; setting up Client with base URL, Headers and then POST
我在纠正我的语法时遇到问题,我将不胜感激;我正在尝试设置包含基础 URL 和一些必要的 header 信息(包括自定义安全令牌)
的客户端
下一步是POST到网络服务,:
$baseServiceURL = ['base_uri' => 'http://127.0.0.1:8080/service/v1/ws//something/update'];
$theHeaders = ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'];
$updateRequestClient = new Client($baseServiceURL, array(
"request.options" => array(
"headers" => $theHeaders
)
));
//var 1 coming from elsewhere
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';
// json name/value pairs
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;
//send
$updateRequestResponse = $updateRequestClient->post([ 'body' => json_encode($updateBody) ]);
//response 200??
$responseCode = $updateRequestResponse->getStatusCode();
if ($responseCode == "200") {
echo ("SUCCESS");
}
我的 HTML 端出现以下错误:
Warning: parse_url() expects parameter 1 to be string, array given in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 51
Catchable fatal error: Argument 1 passed to GuzzleHttp\Psr7\Uri::applyParts() must be of the type array, null given, called in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 55 and defined in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 410
如果我将 URL 更改为字符串 $baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update');
,我会得到:
Catchable fatal error: Argument 1 passed to
GuzzleHttp\Client::__construct() must be of the type array, string
given, called in C:\xampp\htdocs\SSQueryTool\updateDoctor.php on line
79 and defined in C:\xampp\vendor\guzzlehttp\guzzle\src\Client.php on
line 62
$baseServiceURL = 'http://127.0.0.1:8080/service/v1/ws//something/update';
$theHeaders = ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'];
$updateRequestClient = new Client(array(
'base_uri' => $baseServiceURL,
'headers' => $theHeaders
));
客户端在构造函数中只接受 1 个参数
https://github.com/guzzle/guzzle/blob/master/src/Client.php#L62
经过一整天的努力,我终于开始工作了,请在下面找到我的详细信息:
//PREP PAYLOAD (varNum1 coming from elsewhere)
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';
//THE WEBSERVICE UPDATE BASE URL
$baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update');
$updateRequestClient = new Client(['timeout' => 10000.0,]);
//ASSIGN json name/value pairs to body
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;
//SEND AND SAVE RESULT TO updateRequestResponse //TAKE NOTE OF: JSON_FORCE_OBJECT //
$updateRequestResponse = $updateRequestClient->post($baseServiceURL, ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'], 'body' => json_encode($updateBody, JSON_FORCE_OBJECT) ]);
$requestResponseCode = $updateRequestResponse->getStatusCode(); // 200??
if ($requestResponseCode == "200") {
echo ("SUCCESS");
}
我在纠正我的语法时遇到问题,我将不胜感激;我正在尝试设置包含基础 URL 和一些必要的 header 信息(包括自定义安全令牌)
的客户端下一步是POST到网络服务,:
$baseServiceURL = ['base_uri' => 'http://127.0.0.1:8080/service/v1/ws//something/update'];
$theHeaders = ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'];
$updateRequestClient = new Client($baseServiceURL, array(
"request.options" => array(
"headers" => $theHeaders
)
));
//var 1 coming from elsewhere
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';
// json name/value pairs
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;
//send
$updateRequestResponse = $updateRequestClient->post([ 'body' => json_encode($updateBody) ]);
//response 200??
$responseCode = $updateRequestResponse->getStatusCode();
if ($responseCode == "200") {
echo ("SUCCESS");
}
我的 HTML 端出现以下错误:
Warning: parse_url() expects parameter 1 to be string, array given in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 51
Catchable fatal error: Argument 1 passed to GuzzleHttp\Psr7\Uri::applyParts() must be of the type array, null given, called in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 55 and defined in C:\xampp\vendor\guzzlehttp\psr7\src\Uri.php on line 410
如果我将 URL 更改为字符串 $baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update');
,我会得到:
Catchable fatal error: Argument 1 passed to GuzzleHttp\Client::__construct() must be of the type array, string given, called in C:\xampp\htdocs\SSQueryTool\updateDoctor.php on line 79 and defined in C:\xampp\vendor\guzzlehttp\guzzle\src\Client.php on line 62
$baseServiceURL = 'http://127.0.0.1:8080/service/v1/ws//something/update';
$theHeaders = ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'];
$updateRequestClient = new Client(array(
'base_uri' => $baseServiceURL,
'headers' => $theHeaders
));
客户端在构造函数中只接受 1 个参数 https://github.com/guzzle/guzzle/blob/master/src/Client.php#L62
经过一整天的努力,我终于开始工作了,请在下面找到我的详细信息:
//PREP PAYLOAD (varNum1 coming from elsewhere)
$varNum2 = $q;
$varNum3 = $w;
$varNum4 = $e;
$varNum5 = $r;
$varNum6 = $t;
$varNum7 = 'me';
//THE WEBSERVICE UPDATE BASE URL
$baseServiceURL = (string)('http://127.0.0.1:8080/service/v1/ws//something/update');
$updateRequestClient = new Client(['timeout' => 10000.0,]);
//ASSIGN json name/value pairs to body
$updateBody['name1'] = $varNum1;
$updateBody['name2'] = $varNum2;
$updateBody['name3'] = $varNum3;
$updateBody['name4'] = $varNum4;
$updateBody['name5'] = $varNum5;
$updateBody['name6'] = $varNum6;
$updateBody['name7'] = $varNum7;
//SEND AND SAVE RESULT TO updateRequestResponse //TAKE NOTE OF: JSON_FORCE_OBJECT //
$updateRequestResponse = $updateRequestClient->post($baseServiceURL, ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json', 'token' => 'test-token'], 'body' => json_encode($updateBody, JSON_FORCE_OBJECT) ]);
$requestResponseCode = $updateRequestResponse->getStatusCode(); // 200??
if ($requestResponseCode == "200") {
echo ("SUCCESS");
}