-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06demo-post-request.php
More file actions
53 lines (37 loc) · 1.36 KB
/
06demo-post-request.php
File metadata and controls
53 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* This program demonstrates the use of github api
*/
//using curl for api call
$ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, );
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//using classic personal access token for authentication
$headers = [
"Accept: application/vnd.github+json",
"Authorization: token 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",
"X-GitHub-Api-Version: 2022-11-28"
];
$body = json_encode([
"name" => "test-repo-created-by-api",
"description" => "repo created by api",
"private" => false // Set to true if you want a private repository
]);
curl_setopt_array($ch, [
CURLOPT_URL => 'https://api.github.com/user/repos',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_USERAGENT => "pdas544", // GitHub API requires a user agent
// CURLOPT_CUSTOMREQUEST => 'POST', // Specify the request method
CURLOPT_POSTFIELDS => $body, // Set the request body
]);
// Execute the cURL request and get the response
// This will return the response as a string instead of outputting it directly
$response = curl_exec($ch);
//get the http status code
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Output the response and status code
//404 : means this repo is not starred by the user
echo "HTTP Status Code: " . $statusCode . "<br/>";
echo $response . "<br/>";