0

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?

1 Answer 1

0

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();
Sign up to request clarification or add additional context in comments.

2 Comments

With the form_params solution do I still need to write the encoding above? where would the $vars variable come from if this isn’t present. $urlencoded = ""; foreach ($vars as $Index => $Value) $urlencoded .= urlencode($Index) . "=" . urlencode($Value) . "&"; $urlencoded = substr($urlencoded, 0, -1);
No, with form_params you just assign array with variables that you want to sent to form_params key and Guzzle does encoding. I used $vars as this was the name of the variable used in original function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.