API概要

未配信タスクの削除を行う

登録済で未配信のタスクの削除を行います。
同時に複数のタスクの削除を行う事は出来ません。

リクエストURL
https://example.com/API/cancel
※正式なURLは御契約後にご連絡致します。

リクエストパラメータ
パラメータ名 データタイプ 必須 説明
login_id string ログイン用ID
password string ログイン用パスワード
id int タスクID

APIからのXMLについて

APIから送信されるXMLの書式は以下の通りとなります。

<?xml version="1.0" encoding="UTF-8"?>
<response xmlns="urn:3hands:3mail">
  <status>OK</status>                          //ステータス
  <code>203</code>                             //コード
  <message>配信キャンセルに成功しました</message> //メッセージ
</response>

サンプルスクリプト

<?php

//APIへ送信するデータ
//$_REQUESTの部分については、可能であれば$_POSTか$_GETを指定してください
//今回はサンプルの為、汎用性を考えて$_REQUESTとします
$post_data = array(
    "login_id" => "login@example.com", //ログインID
    "password" => "password",          //ログインパスワード
    "id" => $_REQUEST['id']            //タスクID
);
   
//APIのHostを設定
$api_host = "example.com";
   
//APIのPathを設定
$api_path = "/API/cancel";

//APIへ接続
$sp = fsockopen('ssl://' . $api_host, 443, $errno, $errstr, 30);

//エラーチェック
if(!$sp){
    print $errno . "<br>" . $errstr;
    exit;
}

//boundary生成
$boundary = "-----" . md5(uniqid());

//header生成
$header  = "POST " . $api_path . " HTTP/1.0\r\n";
$header .= "Host: " . $api_host . "\r\n";
$header .= "Content-type: multipart/form-data, boundary=" . $boundary . "\r\n";
$header .= "Referer: http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "\r\n";
$header .= "Accept: */*\r\n";
   
//body生成
$body = "";

//テキストデータをAPIへ送信できるように変換
foreach ($post_data as $key => $val){
    $body .= "--" . $boundary . "\r\n";
    $body .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n";
    $body .= "\r\n" . $val . "\r\n";
    $body .= "--" . $boundary . "\r\n";
}
   
//ヘッダーにbodyのサイズを追加
$body .= "--" . $boundary . "--\r\n";
$body .= "\r\n\r\n";
   
//ヘッダーにデータ部のサイズを追加
$header .= "Content-length: " . strlen($body) . "\r\n\r\n";

//データ送信
fputs($sp, $header);
fputs($sp, $body);

//データ受信
$buf = "";
$response = fgets($sp);
   
if (substr_count($response, "200 OK") > 0){
    while (!feof($sp)){
        $buf = $buf . fread($sp, 4096);
    }
}else{
    print "データ受信に失敗しました";
    exit;
}
   
//接続終了
fclose($sp);

//結果XMLを取得
$result = substr($buf, strpos($buf, "\r\n\r\n")+4);

//XMLをパースする
$result_xml = simplexml_load_string($result);

//結果を取得し表示
print "status:"  . $result_xml->status  . "<br>"; //ステータス
print "code:"    . $result_xml->code    . "<br>"; //コード
print "message:" . $result_xml->message . "<br>"; //メッセージ

exit;

?>

サンプルスクリプト解説

APIへ送信するデータ及び、APIのURLについて設定を行います。

//APIへ送信するデータ
//$_REQUESTの部分については、可能であれば$_POSTか$_GETを指定してください
//今回はサンプルの為、汎用性を考えて$_REQUESTとします
$post_data = array(
    "login_id" => "login@example.com", //ログインID
    "password" => "password",          //ログインパスワード
    "id" => $_REQUEST['id']            //タスクID
);
   
//APIのHostを設定
$api_host = "example.com";
   
//APIのPathを設定
$api_path = "/API/cancel";

APIへソケットを使って接続を行います。

//APIへ接続
$sp = fsockopen('ssl://' . $api_host, 443, $errno, $errstr, 30);

//エラーチェック
if(!$sp){
    print $errno . "<br>" . $errstr;
    exit;
}

APIへ送信するデータの作成を行います。

//boundary生成
$boundary = "-----" . md5(uniqid());

//header生成
$header  = "POST " . $api_path . " HTTP/1.0\r\n";
$header .= "Host: " . $api_host . "\r\n";
$header .= "Content-type: multipart/form-data, boundary=" . $boundary . "\r\n";
$header .= "Referer: http://" . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . "\r\n";
$header .= "Accept: */*\r\n";
   
//body生成
$body = "";

//テキストデータをAPIへ送信できるように変換
foreach ($post_data as $key => $val){
    $body .= "--" . $boundary . "\r\n";
    $body .= "Content-Disposition: form-data; name=\"" . $key . "\"\r\n";
    $body .= "\r\n" . $val . "\r\n";
    $body .= "--" . $boundary . "\r\n";
}
   
//ヘッダーにbodyのサイズを追加
$body .= "--" . $boundary . "--\r\n";
$body .= "\r\n\r\n";
   
//ヘッダーにデータ部のサイズを追加
$header .= "Content-length: " . strlen($body) . "\r\n\r\n";

APIへデータの送信を行います。

//データ送信
fputs($sp, $header);
fputs($sp, $body);

データの受信を行います。

ヘッダーが "200 OK" 以外の場合はエラーとなります。

//データ受信
$buf = "";
$response = fgets($sp);
   
if (substr_count($response, "200 OK") > 0){
    while (!feof($sp)){
        $buf = $buf . fread($sp, 4096);
    }
}else{
    print "データ受信に失敗しました";
    exit;
}
   
//接続終了
fclose($sp);

受信したデータからXML部分のみを取得後パースを行います。

//結果XMLを取得
$result = substr($buf, strpos($buf, "\r\n\r\n")+4);

//XMLをパースする
$result_xml = simplexml_load_string($result);

結果の基本項目は以下の方法で取得できます。

今回はサンプルなので取得結果をそのまま表示します。

//結果を取得し表示
print "status:"  . $result_xml->status  . "<br>"; //ステータス
print "code:"    . $result_xml->code    . "<br>"; //コード
print "message:" . $result_xml->message . "<br>"; //メッセージ

サンプルスクリプトの実行結果は以下の通りとなります。

status:OK
code:203
message:配信キャンセルに成功しました

スクリプト実行後の処理について

実際にはXMLを取得後、ステータス及びコードに合わせて処理を行ってください。(正常終了、エラー表示等)

前のページへ戻る

online-order
3HANDS Information お問い合わせはこちら