项目中服务器配置导致CURL请求无法使用。故改用file_get_contents。

function httpPostMethod($url, $data = [], $json = false)
    {
        if ($json) {
            $str = 'application/json';
            $data = json_encode($data);
        } else {
            $str = 'application/x-www-form-urlencoded';
            $data = http_build_query($data);
        }
        $options['http'] = array(
            'timeout' => 5,
            'method' => 'POST',
            'header' => "Content-Type: $str;charset=utf-8",
            'content' => $data,
        );
        $context = stream_context_create($options);
        return file_get_contents($url, false, $context);
    }

使用示例:


$request_url="https://www.fakedomainname.co";
$post_data = array(
        "expire_seconds" => 360,
        "action_info" => array(
            "scene" => array(
                "id" => 296,
            ),
        ),
    );

$res = httpPostMethod($request_url, $post_data,true);
本篇文章相关标签: