我正在尝试对Adforms API进行身份验证,但在尝试时出现错误。
任何人都可以指出我的方向是错的吗?以下是一些细节: - 该脚本正在全新安装的EasyPHP上运行(启用了cUrl)。 - 从另一台计算机启动相同的脚本,它运行得很好(它在Mac上运行 - 我在Windows上)。 - 当然,下面的用户和密码是假的。
代码因此错误而失败:
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
这是我的代码:
<?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();
非常感谢任何帮助!
谢谢, 丁
答案 0 :(得分:0)
检查$ result是否有错误
$result = curl_exec($curl);
if($result === false) {
echo curl_error($curl);
echo curl_errno($curl);
}
或更好的方式:curl_exec() always returns false