我正在使用teamviewer API编写web_app。根据文档,它使用HTTP请求(GET, POST等)工作。
我不知道如何使用CURL在PHP上发出请求。文档要求这样做:
POST /api/v1/oauth2/token HTTP/1.1
Host: webapi.teamviewer.com
Content-Type: application/x-www-form-urlencoded
所以我写这个
$url = 'webapi.teamviewer.com';
$headers = array(
'GET /api/v1/oauth2/authorize HTTP/1.1',
'Content-type: application/x-www-form-urlencoded'
);
//data
$data = [
'response_type' => 'authorization_code',
'redirect_uri' => 'https%3A%2F%2FTesting%2Ecom%2F',
'client_id' => '65671-XDdsxUNyxGmskcJHQgLC'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
我只是不知道如何指示请求HTTP/1.1
和路径/api/v1/oauth2/authorize
。我把它放在页眉中,但我确定它不会出现在那里。
我曾经在嵌入式C上工作,我记得首先你连接到主机,然后写GET路径HTTP/1.1作为命令。
您的$url
应该是完整的路径,就像一个正常的URL。所以应该是
$url = 'webapi.teamviewer.com/api/v1/oauth2/authorize';
您可以从$headers
中删除GET ...
部分,因为URL(路径)不在那里。
之后就可以了。如果您想了解更多关于curl连接的信息,请添加以下选项:
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl选项的完整列表在curl_setopt
的PHP手册页。