I'm trying to authenticate against Adforms API, but am getting an error when I try.
Can anyone point me in the right direction as to what is wrong? Here are some details: - The script is being run on a fresh install of EasyPHP (with cUrl enabled). - The same script is being launched from another computer, on which it works just fine (it runs on a mac - I'm on Windows). - User and password below are fake, of course.
The code fails with this error:
Fatal error: Uncaught exception 'Exception' with message 'We did not get a HTTP 200 when retrieving a ticket. We recieved this: { "url": "https:\/\/api.adform.com\/Services\/Security\/Login", "content_type": null, "http_code": 0, "header_size": 0, "request_size": 0, "filetime": -1, "ssl_verify_result": 1, "redirect_count": 0, "total_time": 0.187, "namelookup_time": 0.14, "connect_time": 0.156, "pretransfer_time": 0, "size_upload": 0, "size_download": 0, "speed_download": 0, "speed_upload": 0, "download_content_length": -1, "upload_content_length": -1, "starttransfer_time": 0, "redirect_time": 0, "redirect_url": "", "primary_ip": "37.157.5.109", "certinfo": [], "primary_port": 443, "local_ip": "192.168.1.7", "local_port": 49460 }false' in C:\Users\Mikkel\PHP\adformdsp\adf-ticket.php:60 Stack trace: #0 C:\Users\Mikkel\PHP\adformdsp\adf-ticket.php(66): Adform->ticket() #1 {main} thrown in C:\Users\Mikkel\PHP\adformdsp\adf-ticket.php on line 60
Here is my code:
<?php
class Adform
{
protected $user;
protected $pass;
public function __construct($user, $pass)
{
$this->user = $user;
$this->pass = $pass;
}
# Authenticate to Adform by retrieving a ticket
public function ticket()
{
$curl = curl_init();
$credentials = json_encode(array(
'UserName' => $this->user,
'Password' => $this->pass
));
$options = array(
CURLOPT_URL => 'https://api.adform.com/Services/Security/Login',
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $credentials,
CURLOPT_HEADER => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_RETURNTRANSFER => TRUE,
);
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
if(curl_getinfo($curl, CURLINFO_HTTP_CODE) == '200')
{
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = json_decode(substr($result, $header_size), TRUE);
curl_close($curl);
if(array_key_exists('Ticket', $body))
{
# We successfully retrieved a ticket!
return $body['Ticket'];
}
else
{
# We connected to Adforms server but did not get a ticket..
throw new Exception('We connected to Adforms server but did not retrieve a ticket. We recieved this: ' . $result);
}
}
else
{
#We did not get a HTTP 200 from the request to Adform..
throw new Exception('We did not get a HTTP 200 when retrieving a ticket. We recieved this: ' . json_encode(curl_getinfo($curl), JSON_PRETTY_PRINT) . json_encode($result, JSON_PRETTY_PRINT));
}
}
}
$adform = new Adform("notsharing","thatwithyouguys");
echo $adform->ticket();
Any help would be much appreciated!
Thanks, Ding