1

私はほとんどの場合、PHP の世界に不慣れです。現在、Silverpop の API をページのフォームに接続しようとしています。以下は私がこれまでに持っているコードです。

問題は、認証は完璧に機能しますが、API が必要とする XML を POST しようとすると、常に「セッションの有効期限が切れているか無効です」というエラーが返されることです。エラーが発生するコード内の領域をマークします。

過去半日を調査に費やしましたが、何が間違っているのか、何が欠けているのかわかりません。cURL のような 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 #///
?>
4

3 に答える 3

5

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

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);
于 2014-02-21T17:30:16.810 に答える
0

セッションを維持するには、Cookie を送り返す必要があると思います。このオプションを 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独自のパスに置き換える必要があります。

于 2013-11-08T14:32:01.143 に答える