Last active
June 17, 2024 19:18
-
-
Save utilmind/583ad451190bf32072615773a71b851c to your computer and use it in GitHub Desktop.
Example of requesting Ad-ID API using POST method
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
<?php | |
$data_to_post = [ | |
'username' => 'user@email', | |
'password' => '', // not specified in this demo | |
//'start' => 0, | |
//'number_to_return' => 2, | |
//'gid' => 1, | |
]; | |
try { | |
echo "Connecting to https://app.ad-id.org/adid_services/code/?format=json\n"; | |
if (!$ch = curl_init('https://app.ad-id.org/adid_services/code/?format=json')) { | |
throw new Exception('Failed to initialize curl.'); | |
} | |
// Don't require SSL connection. It's okay for testing purposes, but should not be allowed in production. | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt_array($ch, [ | |
CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
CURLOPT_MAXREDIRS => 5, // if http server gives redirection responce | |
CURLOPT_TIMEOUT => 30, // seconds. No need to wait more. | |
CURLOPT_ENCODING => 'gzip', // support encrypted content | |
CURLOPT_RETURNTRANSFER => true, // return response as variable instead of output to console | |
]); | |
echo "Using POST request method.\n"; | |
curl_setopt($ch, CURLOPT_POST, true); | |
echo 'POSTing data: '.http_build_query($data_to_post)."\n"; | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data_to_post)); | |
$response_body = curl_exec($ch); | |
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); // integer | |
}finally { | |
curl_close($ch); | |
} | |
echo "\nGot response. HTTP status: $http_status\n"; | |
echo "Response is: ".print_r($response_body, true)."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment