使用 PHP 函数发布到 API 时如何查看响应代码?

问题描述 投票:0回答:1

我希望有人能给我一些好的指导,因为我在网上找不到正确的信息。 我一直遵循 API 文档的说明。 当在 Wordpress 中下订单时,我想将我的数据发布到外部 API。 大部分情况下一切都很顺利。但是,我不明白我应该如何看到响应代码?

例如,我应该得到这样的回复:

{  
  "response":52
}

我做了很多谷歌搜索,听起来大多数 http 响应都显示在浏览器的开发人员工具中。然而,听起来在我的特殊情况下,这是服务器端。 这些响应是否记录在服务器上? 我是否应该添加任何代码来帮助我在测试期间查看响应?

这是我用于 API 的基本代码示例:

<?php

add_action( 'woocommerce_new_order', 'create_new_external_order', 10, 2 );

function create_new_external_order ( $order_id, $order ){
            function sendRequest($url, $type = 'get', $params = [], $json = true) {
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

                if ($type == 'post' || $type == 'put') {
                    curl_setopt($ch, CURLOPT_POST, true);

                    if ($json) {
                        $params = json_encode($params);

                        curl_setopt($ch, CURLOPT_HTTPHEADER, [
                            'Content-Type: application/json',
                            'Content-Length: ' . strlen($params)
                        ]);

                        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                    } else {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
                    }
                }

                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
                curl_setopt($ch, CURLOPT_USERAGENT, 'Poster (http://my-api-base-url.com)');

                $data = curl_exec($ch);
                curl_close($ch);

                return $data;
            }

            $url = 'https://my-api-base-url.com/api/blahblahblah' 
                . '?format=json'
                . '&token=xxxxxxxxx';

            $incoming_order = [
                'spot_id'   => 1,
                'phone'     => '+0123456789',
                'products'  => [
                    [
                        'product_id' => 1,
                        'count'      => 5
                    ],
                ],
            ];

            $data = sendRequest($url, 'post', $incoming_order);
}

?>
php wordpress api rest http-headers
1个回答
0
投票

要在 PHP 代码中处理来自外部 API 的响应代码,您可以修改 sendRequest 函数以捕获和处理 HTTP 响应代码。这是代码的更新版本,其中包含解释更改的注释:

function sendRequest($url, $type = 'get', $params = [], $json = true) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    if ($type == 'post' || $type == 'put') {
        curl_setopt($ch, CURLOPT_POST, true);

        if ($json) {
            $params = json_encode($params);

            curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Content-Type: application/json',
                'Content-Length: ' . strlen($params)
            ]);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        }
    }

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Poster (http://my-api-base-url.com)');

    // Execute the request and capture both response data and HTTP response code
    $data = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    // Close the cURL session
    curl_close($ch);

    // Return both response data and HTTP response code
    return ['data' => $data, 'http_code' => $http_code];
}

// Usage example:
$response = sendRequest($url, 'post', $incoming_order);

// Check the HTTP response code
if ($response['http_code'] == 200) {
    // Successful request
    $responseData = json_decode($response['data'], true);
    // Process $responseData as needed
} else {
    // Handle the error
    echo "Error: HTTP response code " . $response['http_code'];
}

经过此修改,sendRequest 函数现在返回一个包含响应数据和 HTTP 响应代码的数组。然后,您可以在主代码中检查 HTTP 响应代码并相应地处理任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.