简体   繁体   中英

Setting a path HTTP GET Request using curl on PHP, for web API

I'm working on a web_app using teamviewer API. According to the documentation , it works using HTTP requests (GET, POST, etc..).

I don't get how to make a request on PHP using CURL. The documentation said to do this:

POST /api/v1/oauth2/token HTTP/1.1
Host: webapi.teamviewer.com
Content-Type: application/x-www-form-urlencoded

so I wrote this

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

I just can't figure out how to indicate the request the HTTP/1.1 and the path /api/v1/oauth2/authorize . I'm putting it in the header but I'm sure it doesn't go there.

I use to work on embedded C and I remember that first you connect to the host and then write GET path HTTP/1.1 as a command.

Your $url should be the full path, just like a normal URL. So it would be

$url = 'webapi.teamviewer.com/api/v1/oauth2/authorize';

And you can remove the GET ... part from $headers , because the URL (path) doesn't go in there.

That should work after that. If you want to see more info about the curl connection and what's going on, add this option:

curl_setopt($ch, CURLOPT_VERBOSE, true);

The full list of curl options is at the PHP manual page on curl_setopt .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM