首页 > 解决方案 > 我收到一个 POST 请求的奇怪参数错误

问题描述

所以这是我在尝试向 streamlabs api 发出 POST 请求时遇到的错误。我已经把 pastebin 放到了下面的上帝那里。

我很可能是那么愚蠢。谢谢

编辑:即使使用 $code = $_GET['code']; 它仍然返回相同的错误。我不确定为什么。可能是接收器的api有问题吗?我是 php 和 POST / GET 调用的新手。通常我通过 Ajax 制作它们,但它总是返回一个 CORS 错误,因为它们强制使用 php 以确保安全。它仅供个人使用,不会被其他任何人使用。

解决方案:我不确定如何有效地使用堆栈溢出,所以我提前道歉,但解决方案在 Drew010 的评论中!

编码:

<?php 

$code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_URL);
$client_id = 'Removed For Security'; // Your client id
$client_secret = 'Removed For Security'; // Your secret
$redirect_uri = "http://simpservers.co.uk/supporters/"; // Your redirect uri

$url = 'https://streamlabs.com/api/v1.0/token';

# Our new data
$data = array('grant_type' => 'authorization_code', 'client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri, 'code' => strval($code));
# Create a connection
$url = 'https://streamlabs.com/api/v1.0/token';

//initialize cURL
$handle = curl_init($url);

//post values
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);

//set to return the response
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

//execute
$response = (curl_exec( $handle ));

print_r($data);

echo $response;

数据:

(
    [grant_type] => authorization_code
    [client_id] => Removed For Security
    [client_secret] => Removed For Security
    [redirect_uri] => http://simpservers.co.uk/supporters/
    [code] => 4M8WUq97OzeBgL3O41tmVEb4dY9HAy3zdeRQWFFg
)

The response:

    {
        "error":"invalid_request",
        "error_description":"The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the \"code\" parameter."
    }

标签: phpcurlpost

解决方案


推荐阅读