Skip to content
Advertisement

Convert PHP HTTP request to Guzzle

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.0rn";
        $headers .= "Content-Type: application/x-www-form-urlencodedrn";
        $headers .= "Host: secure.test.comrn";
        $headers .= "Content-Length: " . strlen($urlencoded)

        $fp = fsockopen($server, $port, $errno, $errstr, 20);  // returns file pointer
        if (!$fp) return "ERROR: fsockopen failed.rnError 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?

Advertisement

Answer

If you want to send $vars as a body of POST request, you need to set up body property.

$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,
]);

Guzzle can URLencode $vars for you if you use form_params instead of body.

$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,
]);

To read server’s response you need to call getBody on the $response and cast it to string. You will have there exactly the same as return value from your original http_post function.

$resultFromServer = (string) $response->getBody();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement