将 PHP HTTP 请求转换为 Guzzle
Convert PHP HTTP request to Guzzle
我目前有一个旧的 PHP 页面,它对外部 API 执行 post 请求,但我想将其转换为 Guzzle 以整理它向上,但我不确定我是否在正确的路线上。
PHP POST
function http_post($server, $port, $url, $vars)
{
// get urlencoded vesion of $vars array
$urlencoded = "";
foreach ($vars as $Index => $Value)
$urlencoded .= urlencode($Index) . "=" . urlencode($Value) . "&";
$urlencoded = substr($urlencoded, 0, -1);
$headers = "POST $url HTTP/1.0\r\n";
$headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers .= "Host: secure.test.com\r\n";
$headers .= "Content-Length: " . strlen($urlencoded)
$fp = fsockopen($server, $port, $errno, $errstr, 20); // returns file pointer
if (!$fp) return "ERROR: fsockopen failed.\r\nError no: $errno - $errstr"; // if cannot open socket then display error message
fputs($fp, $headers);
fputs($fp, $urlencoded);
$ret = "";
while (!feof($fp)) $ret .= fgets($fp, 1024);
fclose($fp);
return $ret;
}
检索PHP响应
以下是您检索响应的方式,您可以在其中使用 $_POST 字段
$response = http_post("https://secure.test", 443, "/callback/test.php", $_POST);
Guzzle 尝试POST
$client = new Client();
$request = $client->request('POST','https://secure.test.com/callback/test.php', [
// not sure how to pass the $vars from the PHP file
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
]
]);
$request->getBody()->getContents();
Guzzle 尝试 GET
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
然后我如何从响应中获取特定字段?
根据我上面的尝试,我在正确的路线上吗?
如果你想发送$vars
作为POST请求的主体,你需要设置body
属性.
$client = new Client();
// get urlencoded vesion of $vars array
$urlencoded = "";
foreach ($vars as $Index => $Value)
$urlencoded .= urlencode($Index) . "=" . urlencode($Value) . "&";
$urlencoded = substr($urlencoded, 0, -1);
$response = $client->request('POST','https://secure.test.com/callback/test.php', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
],
'body' => $urlencoded,
]);
如果您使用 form_params
而不是 body
,Guzzle 可以为您进行 URLencode $vars
。
$response = $client->request('POST','https://secure.test.com/callback/test.php', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
],
'form_params' => $vars,
]);
要读取服务器的响应,您需要在 $response
上调用 getBody
并将其转换为 string
。您将获得与原始 http_post
函数中的 return 值完全相同的值。
$resultFromServer = (string) $response->getBody();
我目前有一个旧的 PHP 页面,它对外部 API 执行 post 请求,但我想将其转换为 Guzzle 以整理它向上,但我不确定我是否在正确的路线上。
PHP POST
function http_post($server, $port, $url, $vars)
{
// get urlencoded vesion of $vars array
$urlencoded = "";
foreach ($vars as $Index => $Value)
$urlencoded .= urlencode($Index) . "=" . urlencode($Value) . "&";
$urlencoded = substr($urlencoded, 0, -1);
$headers = "POST $url HTTP/1.0\r\n";
$headers .= "Content-Type: application/x-www-form-urlencoded\r\n";
$headers .= "Host: secure.test.com\r\n";
$headers .= "Content-Length: " . strlen($urlencoded)
$fp = fsockopen($server, $port, $errno, $errstr, 20); // returns file pointer
if (!$fp) return "ERROR: fsockopen failed.\r\nError no: $errno - $errstr"; // if cannot open socket then display error message
fputs($fp, $headers);
fputs($fp, $urlencoded);
$ret = "";
while (!feof($fp)) $ret .= fgets($fp, 1024);
fclose($fp);
return $ret;
}
检索PHP响应
以下是您检索响应的方式,您可以在其中使用 $_POST 字段
$response = http_post("https://secure.test", 443, "/callback/test.php", $_POST);
Guzzle 尝试POST
$client = new Client();
$request = $client->request('POST','https://secure.test.com/callback/test.php', [
// not sure how to pass the $vars from the PHP file
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
]
]);
$request->getBody()->getContents();
Guzzle 尝试 GET
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
然后我如何从响应中获取特定字段?
根据我上面的尝试,我在正确的路线上吗?
如果你想发送$vars
作为POST请求的主体,你需要设置body
属性.
$client = new Client();
// get urlencoded vesion of $vars array
$urlencoded = "";
foreach ($vars as $Index => $Value)
$urlencoded .= urlencode($Index) . "=" . urlencode($Value) . "&";
$urlencoded = substr($urlencoded, 0, -1);
$response = $client->request('POST','https://secure.test.com/callback/test.php', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
],
'body' => $urlencoded,
]);
如果您使用 form_params
而不是 body
,Guzzle 可以为您进行 URLencode $vars
。
$response = $client->request('POST','https://secure.test.com/callback/test.php', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'Host' => 'secure.test.com'
],
'form_params' => $vars,
]);
要读取服务器的响应,您需要在 $response
上调用 getBody
并将其转换为 string
。您将获得与原始 http_post
函数中的 return 值完全相同的值。
$resultFromServer = (string) $response->getBody();