-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from rogers-obrien-rad/feature/no-ref/dc-php
Feature/no ref/dc php
- Loading branch information
Showing
12 changed files
with
330 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,4 +217,6 @@ replay_pid* | |
# VS Code | ||
*.code-workspace | ||
|
||
|
||
# PHP | ||
*vendor* | ||
*.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
require 'get_access_token.php'; | ||
|
||
function addAttachment($access_token, $company_id, $project_id, $direct_cost_id, $attachments = []) { | ||
$url = "https://api.procore.com/rest/v1.1/projects/$project_id/direct_costs/$direct_cost_id"; | ||
|
||
$headers = [ | ||
"Authorization: Bearer $access_token", | ||
"Procore-Company-Id: $company_id", | ||
"Accept: application/json" | ||
]; | ||
|
||
$postFields = []; | ||
|
||
// Create a new finfo resource | ||
$finfo = finfo_open(FILEINFO_MIME_TYPE); | ||
|
||
// Prepare attachments | ||
foreach ($attachments as $index => $attachment) { | ||
$mime_type = finfo_file($finfo, $attachment); | ||
if ($mime_type === false) { | ||
$mime_type = 'application/octet-stream'; | ||
} | ||
|
||
$postFields["attachments[$index]"] = new CURLFile($attachment, $mime_type, basename($attachment)); | ||
} | ||
|
||
// Close the finfo resource | ||
finfo_close($finfo); | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); | ||
|
||
$response = curl_exec($ch); | ||
|
||
if (curl_errno($ch)) { | ||
echo 'Error:' . curl_error($ch); | ||
return null; | ||
} | ||
|
||
curl_close($ch); | ||
|
||
return json_decode($response, true); | ||
} | ||
|
||
// Main execution | ||
$access_token = getAccessToken($client_id, $client_secret); | ||
|
||
$company_id = 8089; | ||
$project_id = 2783683; | ||
$direct_cost_id = 95483758; | ||
$attachments = [ | ||
"C:\Users\hfritz\OneDrive - RO\Documents\packages\ProPyCore\php\direct_costs_module.pdf", | ||
]; | ||
|
||
$response = addAttachment($access_token, $company_id, $project_id, $direct_cost_id, $attachments); | ||
echo "Response from adding attachment: \n"; | ||
print_r($response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"require": { | ||
"vlucas/phpdotenv": "^5.6" | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
|
||
$dotenv = Dotenv\Dotenv::createImmutable(dirname(__DIR__)); | ||
$dotenv->load(); | ||
|
||
$client_id = getenv('CLIENT_ID'); | ||
$client_secret = getenv('CLIENT_SECRET'); | ||
|
||
function getAccessToken($client_id, $client_secret) { | ||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, "https://login.procore.com/oauth/token"); | ||
curl_setopt($ch, CURLOPT_POST, 1); | ||
|
||
$postFields = [ | ||
'grant_type' => 'client_credentials', | ||
'client_id' => $client_id, | ||
'client_secret' => $client_secret, | ||
'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob' | ||
]; | ||
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
|
||
$response = curl_exec($ch); | ||
|
||
if (curl_errno($ch)) { | ||
echo 'Error:' . curl_error($ch); | ||
return null; | ||
} | ||
|
||
$response_data = json_decode($response, true); | ||
curl_close($ch); | ||
|
||
return $response_data['access_token']; | ||
} | ||
|
||
// Main execution | ||
$access_token = getAccessToken($client_id, $client_secret); | ||
|
||
if ($access_token) { | ||
echo "Successfully obtained access token"; | ||
} else { | ||
echo "Failed to obtain access token."; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
require 'get_access_token.php'; // Ensure this path is correct based on your directory structure | ||
|
||
function getCompanies($access_token, $page = 1, $per_page = 100) { | ||
$endpoint = "https://api.procore.com/rest/v1.0/companies"; | ||
$params = [ | ||
"page" => $page, | ||
"per_page" => $per_page, | ||
"include_free_companies" => "true" | ||
]; | ||
|
||
$query = http_build_query($params); | ||
$url = $endpoint . '?' . $query; | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
'Authorization: Bearer ' . $access_token, | ||
'Content-Type: application/json' | ||
]); | ||
|
||
$response = curl_exec($ch); | ||
|
||
if (curl_errno($ch)) { | ||
echo 'Error:' . curl_error($ch); | ||
return null; | ||
} | ||
|
||
$companies = json_decode($response, true); | ||
curl_close($ch); | ||
|
||
return $companies; | ||
} | ||
|
||
// Main execution | ||
$access_token = getAccessToken($client_id, $client_secret); | ||
$companies = getCompanies($access_token); | ||
|
||
echo "Companies: \n"; | ||
print_r($companies); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
require 'get_access_token.php'; // Ensure this path is correct based on your directory structure | ||
|
||
function getDirectCosts($access_token, $company_id, $project_id, $page = 1, $per_page = 100) { | ||
$endpoint = "https://api.procore.com/rest/v1.1/projects/$project_id/direct_costs"; | ||
$direct_costs = []; | ||
$n_costs = 1; | ||
|
||
while ($n_costs > 0) { | ||
$params = [ | ||
"page" => $page, | ||
"per_page" => $per_page | ||
]; | ||
|
||
$query = http_build_query($params); | ||
$url = $endpoint . '?' . $query; | ||
|
||
$ch = curl_init(); | ||
|
||
curl_setopt($ch, CURLOPT_URL, $url); | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | ||
'Authorization: Bearer ' . $access_token, | ||
'Content-Type: application/json', | ||
'Procore-Company-Id: ' . $company_id | ||
]); | ||
|
||
$response = curl_exec($ch); | ||
|
||
if (curl_errno($ch)) { | ||
echo 'Error:' . curl_error($ch); | ||
return null; | ||
} | ||
|
||
$costs_per_page = json_decode($response, true); | ||
$n_costs = count($costs_per_page); | ||
|
||
$direct_costs = array_merge($direct_costs, $costs_per_page); | ||
|
||
$page++; | ||
curl_close($ch); | ||
} | ||
|
||
return $direct_costs; | ||
} | ||
|
||
// Main execution | ||
$access_token = getAccessToken($client_id, $client_secret); | ||
|
||
$company_id = 8089; | ||
$project_id = 2783683; | ||
$direct_costs = getDirectCosts($access_token, $company_id, $project_id); | ||
echo "Direct Costs: \n"; | ||
print_r($direct_costs); | ||
?> |
Oops, something went wrong.