Php 通过curl进行的Silverpop数据传输返回会话已过期或无效

Php 通过curl进行的Silverpop数据传输返回会话已过期或无效,php,curl,Php,Curl,我对PHP的世界基本上是新手。我目前正在尝试将Silverpop的API与我们页面上的表单进行接口。下面是我到目前为止的代码 问题是,虽然身份验证工作得很好,但每当我尝试发布API所需的XML时,总会返回一个错误,上面写着:“会话已过期或无效。”我会在代码中标记出现错误的区域 我已经花了半天的时间研究,我不知道我做错了什么,或者我可能遗漏了什么。有人含糊其辞地告诉我,它与浏览器中的输出有关,比如cURL之类的prefore-PHP头函数,但这并没有什么帮助,因为我正在一个空白(无html)PHP

我对PHP的世界基本上是新手。我目前正在尝试将Silverpop的API与我们页面上的表单进行接口。下面是我到目前为止的代码

问题是,虽然身份验证工作得很好,但每当我尝试发布API所需的XML时,总会返回一个错误,上面写着:“会话已过期或无效。”我会在代码中标记出现错误的区域

我已经花了半天的时间研究,我不知道我做错了什么,或者我可能遗漏了什么。有人含糊其辞地告诉我,它与浏览器中的输出有关,比如cURL之类的prefore-PHP头函数,但这并没有什么帮助,因为我正在一个空白(无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 #///
?>

我认为您需要发回cookies以保持会话的活动状态。尝试将此选项添加到您的curl请求中

CURLOPT_COOKIEJAR

这是存储cookie的文件名。如果服务器设置了任何选项,它们将被写入该文件,如果该文件不存在,则将创建该文件

CURLOPT_-COOKIEFILE

cookie jar设置是curl写入cookie的位置,但是curl需要单独的设置才能将cookie发送回服务器。这是CURLOPT_COOKIEFILE设置。如果未设置,则不会向服务器发送Cookie。如果文件不存在,则不会发出错误

curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');

您需要将
/tmp/cookies.txt
替换为您自己的路径。

我认为您需要发回cookies以保持会话活动。尝试将此选项添加到您的curl请求中

CURLOPT_COOKIEJAR

这是存储cookie的文件名。如果服务器设置了任何选项,它们将被写入该文件,如果该文件不存在,则将创建该文件

CURLOPT_-COOKIEFILE

cookie jar设置是curl写入cookie的位置,但是curl需要单独的设置才能将cookie发送回服务器。这是CURLOPT_COOKIEFILE设置。如果未设置,则不会向服务器发送Cookie。如果文件不存在,则不会发出错误

curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');

您需要用自己的路径替换
/tmp/cookies.txt

您只能在一小时内请求一定数量的访问令牌。
在您的代码中,看起来您混合了OAuth授权和jsession登录/注销

如果您有一个访问令牌(通过OAuth请求),请将您的访问令牌保存在会话中,因为它的有效期为4小时。 curl头需要在请求中包含令牌:

$curlHeaders[] = "Authorization: Bearer {$accessToken}"; 
当您使用用户名/密码对api进行身份验证时,将使用xml注销命令。 login命令将为您提供一个jsessionid,然后在每个请求中使用它(而不是访问令牌)

我当前使用的脚本将访问令牌与到期时间/日期一起保存在会话中。当Silverpop连接器发出xml命令时,它会检查访问令牌,随后会检查它是否即将过期或已经过期,如果是,则请求一个新的

$getNewToken = true; //assume you need a new access token
//check if the session has an access token
if (!empty($_SESSION['access'])) {

//check access token is present and not expired
if ( ($expiry > (time() + 5)) && (!empty($token)) ) { 

//set current access token in the Connector, no new access token required
SilverpopConnector::getInstance()->setAccessToken($token,$expiry); 
$getNewToken = false;

//if the session wasn't set or token in session invalid/expired:
if ($getNewToken) { 
//get access token & expiry from the Connector (will authenticate if expired/invalid)
$accessToken = SilverpopConnector::getInstance()->getAccessToken();
$tokenExpiry = SilverpopConnector::getInstance()->getAccessTokenExpiry();
//2a. if successful, set in session
//2b. if not successful, check if you're connected/re-connect
我上面提到的Silverpop连接器是由mrmarkfrench在GitHub上创建的脚本。 请退房

希望这有帮助,

Sandra。

您在一小时内只能请求一定数量的访问令牌。
在您的代码中,看起来您混合了OAuth授权和jsession登录/注销

如果您有一个访问令牌(通过OAuth请求),请将您的访问令牌保存在会话中,因为它的有效期为4小时。 curl头需要在请求中包含令牌:

$curlHeaders[] = "Authorization: Bearer {$accessToken}"; 
当您使用用户名/密码对api进行身份验证时,将使用xml注销命令。 login命令将为您提供一个jsessionid,然后在每个请求中使用它(而不是访问令牌)

我当前使用的脚本将访问令牌与到期时间/日期一起保存在会话中。当Silverpop连接器发出xml命令时,它会检查访问令牌,随后会检查它是否即将过期或已经过期,如果是,则请求一个新的

$getNewToken = true; //assume you need a new access token
//check if the session has an access token
if (!empty($_SESSION['access'])) {

//check access token is present and not expired
if ( ($expiry > (time() + 5)) && (!empty($token)) ) { 

//set current access token in the Connector, no new access token required
SilverpopConnector::getInstance()->setAccessToken($token,$expiry); 
$getNewToken = false;

//if the session wasn't set or token in session invalid/expired:
if ($getNewToken) { 
//get access token & expiry from the Connector (will authenticate if expired/invalid)
$accessToken = SilverpopConnector::getInstance()->getAccessToken();
$tokenExpiry = SilverpopConnector::getInstance()->getAccessTokenExpiry();
//2a. if successful, set in session
//2b. if not successful, check if you're connected/re-connect
我上面提到的Silverpop连接器是由mrmarkfrench在GitHub上创建的脚本。 请退房

希望这有帮助,

桑德拉。

我能让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
// 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);
向Silverpop发送XML请求信封:

    // 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
// 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);

我能让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
// 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);
向Silverpop发送XML请求信封:

    // 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
// 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);

我是个好球,但它没有改变任何事情。我想知道我是否正确应用了应用程序令牌。我现在认为这在我的服务器上不是一个会话问题,而在他们这方面更是一个问题,他们由于某种原因无法接受我的令牌,因此无法创建一个“有效”会话。不过,谢谢你的建议。我是一个很好的射手,但这并没有改变任何事情。我想知道我是否正确应用了应用程序令牌。我现在认为这在我的服务器上不是一个会话问题,而在他们这方面更是一个问题,他们由于某种原因无法接受我的令牌,因此无法创建一个“有效”会话。谢谢你的建议,谢谢!我完全忘了发这个,但几个月前我回答了自己的问题。我将编辑我的帖子以包含我的解决方案。谢谢你抽出时间。我希望这能帮助新加入Silverpop API的人。谢谢!我完全忘了发这个,但几个月前我回答了自己的问题。我将编辑我的帖子以包含我的解决方案。谢谢你抽出时间。我希望