I currently have an old PHP page which carries out a post request to an external API but i am wanting to convert this to Guzzle to tidy it up but im not sure if im on the right lines with this.
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;
}
Retrieve PHP response
Below is how you retrieved the response where you could make use of the $_POST fields
$response = http_post("https://secure.test", 443, "/callback/test.php", $_POST);
Guzzle Attempt 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 Attempt GET
$client = new Client();
$request = $client->get('https://secure.test.com/callback/test.php');
$request->getBody()->getContents();
How would i then grab specific fields from the response?
From my attempts above am i on the right lines?