首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >php simplexml与spreadshirt api

php simplexml与spreadshirt api
EN

Stack Overflow用户
提问于 2014-04-11 15:52:49
回答 1查看 257关注 0票数 0

我试图使用Spreadshirt API在他们的平台上创建一个产品,但我遇到了这个奇怪的错误:

代码语言:javascript
运行
复制
Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/anarchoi/public_html/test.php:102 Stack trace: #0 /home/anarchoi/public_html/test.php(102): SimpleXMLElement->__construct('') #1 {main} thrown in /home/anarchoi/public_html/test.php on line 102

大部分代码都是从他们的wiki上复制过来的,所以我真的不明白为什么它不能工作。

我正在寻找帮助,以了解错误是从哪里来的,以及为什么会发生。

代码语言:javascript
运行
复制
$productTypeId = "210";
$printTypeId = "17";
$printColorIds = "13,20";
$productTypeAppearanceId = "1";
$productTypePrintAreaId = "4";
$designId = "10438193";

// 1. Get shop data
$shopUrl = "http://api.spreadshirt.com/api/v1/shops/266497";
$ch = curl_init($shopUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);

$shop = new SimpleXMLElement($result);
$namespaces = $shop->getNamespaces(true);

// 2. Get product type data
$attributes = $shop->productTypes->attributes($namespaces['xlink']);
$productTypeUrl = $attributes->href . "/" . $productTypeId;
$ch = curl_init($productTypeUrl);
// echo "<br>$productTypeUrl<br>";
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);

$productType = new SimpleXMLElement($result);

// 3. Get design data
$attributes = $shop->designs->attributes($namespaces['xlink']);
$designUrl = $attributes->href . "/" . $designId;
$ch = curl_init($designUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);

$design = new SimpleXMLElement($result);

// 4. Prepare product
// get positioning data for selected product type
$printArea = null;
foreach ($productType->printAreas->printArea as $current) {
    if ($current['id'] == $productTypePrintAreaId) {
        $printArea = $current;
    }
}

$product = new SimpleXMLElement(getFileData("product.xml"));
$product->productType['id'] = $productTypeId;
$product->appearance['id'] = $productTypeAppearanceId;
$configuration = $product->configurations->configuration;
$configuration->printArea['id'] = $productTypePrintAreaId;
$configuration->printType['id'] = $printTypeId;
$configuration->offset->x =
          ((doubleval($printArea->boundary->size->width) - doubleval($design->size->width)) / 2);
$configuration->offset->y =
          ((doubleval($printArea->boundary->size->height) - doubleval($design->size->height)) / 4);
$image = $product->configurations->configuration->content->svg->image;
$image['width'] = $design->size->width;
$image['height'] = $design->size->height;
$image['designId'] = $designId;
$image['printColorIds'] = $printColorIds;

// 5. Create product
$attributes = $shop->products->attributes($namespaces['xlink']);
$productsUrl = $attributes->href;

$header = array();
$header[] = createSprdAuthHeader("POST", $productsUrl);
$header[] = "Content-Type: application/xml";

$ch = curl_init("$productsUrl"."?fullData=true");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $product->asXML());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);
$productUrl = parseHttpHeaders($result, "Location");

$ch = curl_init($productUrl);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
$result = curl_exec($ch);
// Close the handle
curl_close($ch);

$product = new SimpleXMLElement($result);
$resource = $product->resources->resource[0];
$attributes = $resource->attributes($namespaces['xlink']);

echo '<html><body>';
echo 'Product available at: <a href="' . $productUrl . '">' . $productUrl . '</a></br>';
echo 'Product image is available at: <a href="' . $attributes->href . '">' . $attributes->href . '</a></br>';
echo '<img src="' . $attributes->href . '?width=1000"/>';
echo '</body></html>';

function createSprdAuthHeader($method, $url) {
    $apiKey = "***";
    $secret = "***";
    $time = time()*1000;

    $data = "$method $url $time";
    $sig = sha1("$data $secret");

    return "Authorization: SprdAuth apiKey=\"$apiKey\", data=\"$data\", sig=\"$sig\"";
}

function parseHttpHeaders( $header, $headername ) {
    $retVal = array();
    $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
    foreach( $fields as $field ) {
        if( preg_match('/('.$headername.'): (.+)/m', $field, $match) ) {
            return $match[2];
        }
    }
    return $retVal;
}

function getFileData($file) {
    $fp = fopen($file, "r");
    $data = "";
    while(!feof($fp)) {
        $data .= fgets($fp, 1024);
    }
    fclose($fp);
    return $data;
}

product.xml = https://www.ni-dieu-ni-maitre.com/product.xml

EN

回答 1

Stack Overflow用户

发布于 2014-09-11 00:36:10

我在实现Spreadshirt时遇到了类似的问题,看起来他们的应用程序接口服务器正在发送(至少是一些)压缩的内容,而不考虑任何Accept-Encoding头文件。你可以通过在字符串被传递给SimpleXMLElement之前使用var_dump来判断它的情况(正如其他人所建议的那样)-如果它是胡言乱语,那么你很可能和我有同样的问题。

CURLOPT_ENCODING的curl选项设置为空字符串('')修复了对我来说,它“神奇”地打开了string响应(有关更多信息,请参阅man page for curl_setopt() )。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23006679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档