我尝试在创建会话(登录)后检索用户的设计但我不断收到解析错误:XML解析错误:文档元素之后的垃圾。我在我的本地机器和服务器端尝试了这个。在服务器端,我只得到一个空白页,因为我认为错误被设置为不显示。
这是我的代码:
$ch = curl_init("http://api.spreadshirt.$tld/api/v1/sessions");
curl_setopt($ch, CURLOPT_HEADER, true); // get the header
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login->asXML());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, '');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
return null;
}
curl_close($ch);
$headers = parseHttpHeaders($result);
$arr = explode('/', $headers[0]['Location']);
define('SESSION_ID', $arr[count($arr) - 1]);
$header = array();
$header[] = createSprdAuthHeader("GET", "http://api.spreadshirt.$tld/api/v1/users/$userId/designs");
$header[] = "Content-Type: application/xml";
$ch = curl_init("http://api.spreadshirt.$tld/api/v1/users/$userId/designs");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, '');
$result = curl_exec($ch);
curl_close($ch);
header('Content-Type: application/xml; charset=utf-8');
echo $result;
function createSprdAuthHeader($method, $url) {
$apiKey = SPREADSHIRT_API_KEY;
$secret = SPREADSHIRT_API_SECRET;
$session = SESSION_ID;
$time = time() * 1000;
$data = "$method $url $time";
$sig = sha1("$data $secret");
return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\", sessionId=\"$session\"";
}