Trying to access an api with a php curl get request. For the get request to work I must first authenticate with a post request, essentially signing in to the platform. I get a correct response from the post request but I can't get the get request to work for me. Here is my code:
<?php
$login_url = "https://publisher-api.company.com/1.0/Publisher/Login";
$user_pswd = array(
"username" => "username1",
"password" => "password1"
);
$report_url = "https://publisher-api.company.com/1.0/Publisher(#####)/Channels/RevenueReport";
function httpGet($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, true);
$output = curl_exec($ch);
if($output === false)
{
echo "Error Number:".curl_errno($ch)."<br>";
echo "Error String:".curl_error($ch);
}
echo $output;
}
function httpPost($url_post, $params, $url_get)
{
foreach($params as $key=>$value) { $params_string .=$key.'='.$value.'&'; }
rtrim($params_string, '&');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_post);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);
$result = curl_exec($ch);
echo $result;
httpGet($url_get);
curl_close($ch);
}
httpPost($login_url, $user_pswd, $report_url);
?>
This is the output that is echoed out:
{"value":{"publisher":{"active":"1","publisher_id":"#####"}}}1{"error":{"code":"SYSTEM.SERVICE.NOT_AUTHORIZED","message":"You are not authorized to use this service. Authenticate first."}}