【问题标题】:Convert PHP HTTP request to Guzzle将 PHP HTTP 请求转换为 Guzzle
【发布时间】:2020-09-20 05:57:35
【问题描述】:

我目前有一个旧的 PHP 页面,它向外部 API 执行发布请求,但我想将其转换为 Guzzle 以整理它,但我不确定我是否在正确的行上。

PHP 发布

 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();

然后我将如何从响应中获取特定字段?

从我上面的尝试来看,我是在正确的路线上吗?

【问题讨论】:

    标签: php http guzzle


    【解决方案1】:

    如果要将$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 可以为您 URL 编码 $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 函数的返回值完全相同的值。

    $resultFromServer = (string) $response->getBody();
    

    【讨论】:

    • 使用form_params解决方案我还需要写上面的编码吗?如果不存在,$vars 变量从何而来。 $urlencoded = ""; foreach ($vars as $Index => $Value) $urlencoded .= urlencode($Index) 。 “=”。 urlencode($Value) 。 "&"; $urlencoded = substr($urlencoded, 0, -1);
    • 不,使用 form_params 您只需将要发送到 form_params 键的变量分配给数组,Guzzle 会进行编码。我使用 $vars 因为这是原始函数中使用的变量的名称。
    猜你喜欢
    • 2015-11-02
    • 2015-06-01
    • 1970-01-01
    • 2017-01-13
    • 2018-10-30
    • 2021-11-30
    • 2019-12-17
    • 2016-12-04
    • 1970-01-01
    相关资源
    最近更新 更多