将PHP HTTP请求转换为Guzzle
我目前有一个旧的PHP页面,它执行一个对外部API的post请求,但我想将其转换为Guzzle来整理它,但我不确定我是否正确使用了它 PHP POST将PHP HTTP请求转换为Guzzle,php,http,guzzle,Php,Http,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 =&
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;
}
$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();
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
检索PHP响应
下面是如何检索响应的,您可以在其中使用$\u POST字段
$response = http_post("https://secure.test", 443, "/callback/test.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;
}
$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();
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
狂饮尝试获得
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;
}
$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();
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
然后如何从响应中获取特定字段
从我上面的尝试中,我是否在正确的行上?如果要将
$vars
作为POST请求主体发送,则需要设置属性
$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,
]);
如果您使用URLencode而不是body
,Guzzle可以为您编码$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\u post
函数返回值完全相同的返回值
$resultFromServer = (string) $response->getBody();
使用form_params解决方案,我还需要编写上面的编码吗?如果不存在$vars变量,那么$vars变量从何而来$urlencoded=“”;foreach($vars as$Index=>$Value)$urlencoded.=urlencode($Index)。"=" . urlencode($Value)。"&"; $urlencoded=substr($urlencoded,0,-1);不,对于form_params,您只需将要发送到form_params键的变量分配给数组,Guzzle进行编码。我使用$vars,因为这是原始函数中使用的变量的名称。