我是PHP新世界的新手.我目前正在尝试将Silverpop的API与我们页面上的表单进行交互.下面是我到目前为止的代码.
问题在于,虽然身份验证工作非常完美,但每当我尝试POST API所需的XML时,我总是会返回一个错误消息:"会话已过期或无效." 我将在代码中标记出现错误的区域.
我花了最后半天研究,我无法弄清楚我做错了什么或者我可能缺少什么.有人告诉我,它与浏览器中的输出有关,在PHP头文件函数之前就像cURL一样,但由于我在一个空白(没有html)的php文件中测试它,所以一直没有什么帮助.
<?php
// Vars
$firstname = 'a';
$lastname = 'a';
$email = 'a@b.com';
// cURL
function curl($url,$header,$postbody) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
///# SILVERPOP API #///
// SILVERPOP API URLs
// get_token uses oauth to retrieve an access token (works just fine)
$get_token = 'https://api1.silverpop.com/oauth/token?*inforemoved*';
$xmlapi = 'https://api1.silverpop.com/XMLAPI?Authorization=';
// XML STRINGS
$xml_post = '<Envelope><Body><AddRecipient><LIST_ID>database#</LIST_ID><CREATED_FROM>1</CREATED_FROM><SEND_AUTOREPLY>true</SEND_AUTOREPLY><UPDATE_IF_FOUND>true</UPDATE_IF_FOUND><COLUMN><NAME>Name</NAME><VALUE>'.$firstname.'</VALUE></COLUMN><COLUMN><NAME>LastName</NAME><VALUE>'.$lastname.'</VALUE></COLUMN><COLUMN><NAME>Email</NAME><VALUE>'.$email.'</VALUE></COLUMN><COLUMN><NAME>Lead Source</NAME><VALUE>Lead_SqueezePage_5Questions</VALUE></COLUMN></AddRecipient></Body></Envelope>';
$xml_done = '<Envelope><Body><Logout/></Body></Envelope>';
// HEADER VALUES
$h_access = 'Content-Type:x-www-form-urlencoded';
$h_api = 'Content-Type:text/xml;charset=UTF-8';
// Get API Access token
$auth = curl($get_token,$h_access,'');
// Pull access_token from the return string
$auth = explode('"', $auth);
for ($i=0; $i < count($auth); $i++) {
if ( $auth[$i] == "access_token" ) {
$access_token = $auth[$i + 2];
break;
}
}
//Append token to URL unless auth failed, then die
if ( $access_token != NULL) {
$xmlapi .= $access_token;
} else {
// Logout API session - SESSION ERROR HERE
$logout = curl($xmlapi,$h_api,$xml_done);
//echo 'Authentication Failed!';
die;
}
// Send Customer Data - SESSION ERROR HERE
$inject = curl($xmlapi,$h_api,$xml_post);
// Logout API Session - SESSION ERROR HERE
$logout = curl($xmlapi,$h_api,$xml_done);
///# END SILVERPOP API #///
?>
Run Code Online (Sandbox Code Playgroud)
我能够让cURL正常工作.我希望下面的代码可以帮助那些第一次使用Silverpop API并且卡住的人.
从Silverpop中检索访问密钥:
// POST Fields
$fields = array(
'client_id' => CLIENT_KEY,
'client_secret' => CLIENT_SECRET,
'refresh_token' => REFRESH_TOKEN,
'grant_type' => 'refresh_token'
);
// Init cURL
$ch = curl_init();
// Set Options
curl_setopt($ch, CURLOPT_URL, ACCESS_KEY_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
// Execute cURL
$result = curl_exec($ch);
// Check for HTTP and sever errors here if you wish
// with curl_getinfo($ch, CURLINFO_HTTP_CODE)
// Close Connection
curl_close($ch);
// Now you can work with the returned $result string
Run Code Online (Sandbox Code Playgroud)
将XML请求信封发送给Silverpop:
// Set POST header
$header = array(
'Content-Type:text/xml;charset=UTF-8','Authorization: Bearer '.$access_key
);
// init cURL
$ch = curl_init();
// set cURL options
curl_setopt($ch, CURLOPT_URL, REQUEST_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, count($xml_post));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post);
Run Code Online (Sandbox Code Playgroud)