AnsweredAssumed Answered

API user stopped working after PHP upgrade On-Demand

Question asked by Joe Carter on Mar 20, 2017
Latest reply on Jul 18, 2017 by pavan agrawal

Hi

 

Since Sugar upgraded to PHP 5.6 a few days ago the standard call function has stopped working.  It now returns 

 

 

function call($url, $oauthtoken = '', $type = 'GET', $arguments = array(), $encodeData = true, $returnHeaders = false)
{
$type = strtoupper($type);

if ($type == 'GET') {
$url .= "?" . http_build_query($arguments);
}

$curl_request = curl_init($url);

if ($type == 'POST') {
curl_setopt($curl_request, CURLOPT_POST, 1);
} elseif ($type == 'PUT') {
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
} elseif ($type == 'DELETE') {
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
}

curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

if (!empty($oauthtoken)) {
$token = array(
"oauth-token: {$oauthtoken}"
);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
}

if (!empty($arguments) && $type !== 'GET') {
if ($encodeData) {
//encode the arguments as JSON
$arguments = json_encode($arguments);
}
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
}

$result = curl_exec($curl_request);

if ($returnHeaders) {
//set headers from response
list($headers, $content) = explode("\r\n\r\n", $result, 2);
foreach (explode("\r\n", $headers) as $header) {
header($header);
}

//return the nonheader data
return trim($content);
}

curl_close($curl_request);

//decode the response from JSON
$response = json_decode($result);

return $response;
}

 

$instance_url = "https://myurl/rest/v10";
$username = "myadminuser";
$password = "myadminpw";

//Login - POST /oauth2/token

$url = $instance_url . "/oauth2/token";

$oauth2_token_arguments = array(
"grant_type" => "password",
"client_id" => "sugar",
"client_secret" => "",
"username" => $username,
"password" => $password,
"platform" => "custom_api"
);

$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);

$url = $instance_url . "/Opportunities/" . $_GET['id'];

$record_response = call($url, $oauth2_token_response->access_token, 'GET');

echo "<pre>";
print_r($record_response);
echo "</pre>";

 

This used to return the correct data whereas now it returns

 

Array (     [error] => need_login     [error_message] => No valid authentication for user. )

Sugar have said make sure "Content-Type: application/json" in CURLOPT_HTTPHEADER but
this doesn't work either.

Has anyone come across this yet and how can I fix it without changing all my code to use a different
way to connect!!

Outcomes