带有spreadshirt api的php simplexml
我试图使用Spreadshirt API在他们的平台上创建产品,但我遇到了一个奇怪的错误:带有spreadshirt api的php simplexml,php,xml,api,simplexml,Php,Xml,Api,Simplexml,我试图使用Spreadshirt API在他们的平台上创建产品,但我遇到了一个奇怪的错误: 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): SimpleXMLE
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
大部分代码都是从他们的维基上复制的,所以我真的不明白为什么它不起作用
我正在寻求帮助,以了解错误的来源和原因
$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;
}
$productTypeId=“210”;
$printTypeId=“17”;
$printColorIds=“13,20”;
$productTypeAppearanceId=“1”;
$productTypePrintAreaId=“4”;
$designId=“10438193”;
// 1. 获取店铺数据
$shopUrl=”http://api.spreadshirt.com/api/v1/shops/266497";
$ch=curl\u init($shopUrl);
curl_setopt($ch,CURLOPT_HTTP_VERSION,curl_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_头,false);
$result=curl\u exec($ch);
//关上把手
卷曲关闭($ch);
$shop=新的simplexmlement($result);
$namespaces=$shop->getNamespaces(true);
// 2. 获取产品类型数据
$attributes=$shop->productTypes->attributes($namespace['xlink']);
$productTypeUrl=$attributes->href。"/" . $productTypeId;
$ch=curl\u init($productTypeUrl);
//echo“
$productTypeUrl
”;
curl_setopt($ch,CURLOPT_HTTP_VERSION,curl_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_头,false);
$result=curl\u exec($ch);
//关上把手
卷曲关闭($ch);
$productType=新的SimpleXMLElement($result);
// 3. 获取设计数据
$attributes=$shop->designs->attributes($namespace['xlink']);
$designUrl=$attributes->href。"/" . $设计ID;
$ch=curl\u init($designUrl);
curl_setopt($ch,CURLOPT_HTTP_VERSION,curl_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_头,false);
$result=curl\u exec($ch);
//关上把手
卷曲关闭($ch);
$design=新的SimpleXMLElement($result);
// 4. 准备产品
//获取所选产品类型的定位数据
$printparea=null;
foreach($productType->printArea->printArea作为$current){
如果($current['id']=$productTypePrintAreaId){
$printparea=$current;
}
}
$product=新的SimpleXMLElement(getFileData(“product.xml”);
$product->productType['id']=$productTypeId;
$product->外观['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. 创造产品
$attributes=$shop->products->attributes($namespace['xlink']);
$productsUrl=$attributes->href;
$header=array();
$header[]=createSprdAuthHeader(“POST”、$productsUrl);
$header[]=“内容类型:应用程序/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_头,true);
$result=curl\u exec($ch);
//关上把手
卷曲关闭($ch);
$productUrl=parseHttpHeaders($result,“Location”);
$ch=curl\u init($productUrl);
curl_setopt($ch,CURLOPT_HTTP_VERSION,curl_HTTP_VERSION_1_1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_头,false);
$result=curl\u exec($ch);
//关上把手
卷曲关闭($ch);
$product=新的SimpleXMLElement($result);
$resource=$product->resources->resource[0];
$attributes=$resource->attributes($namespaces['xlink']);
回声';
echo“产品可在以下网址获得:”;
echo“产品图片可从以下网址获得:”;
echo'href'?宽度=1000“/>”;
回声';
函数createSprdAuthHeader($method,$url){
$apiKey=“***”;
$secret=“***”;
$time=time()*1000;
$data=“$method$url$time”;
$sig=sha1($data$secret);
返回“Authorization:SprdAuth apiKey=\“$apiKey\”,data=\“$data\”,sig=\“$sig\”;
}
函数parseHttpHeaders($header,$headername){
$retVal=array();
$fields=explode(“\r\n”,preg_replace('/\x0D\x0A[\x09\x20]+/','','',$header));
foreach($fields作为$field){
if(preg_匹配('/('.$headername'):(.+)/m',$field,$match)){
返回$match[2];
}
}
返回$retVal;
}
函数getFileData($file){
$fp=fopen($file,“r”);
$data=“”;
而(!feof($fp)){
$data.=fgets($fp,1024);
}
fclose($fp);
返回$data;
}
product.xml=在实现Spreadshirt时,我偶然发现了类似的问题,看起来他们的API服务器正在发送(至少部分)Gzip内容,而不考虑任何
接受编码
标题。您可以通过变量转储
在字符串传递到SimpleXMLElement
之前对其进行排序来判断情况(正如其他人所建议的)——如果这是胡言乱语,很可能你和我有同样的问题
将
CURLOPT_ENCODING
的curl选项设置为空字符串('
)修复了我的问题–它“神奇地”打开了对响应的解压缩(请参阅了解更多信息)。第102行?将传递的数据输出到SimpleXMLElement(),这可能不是您所期望的。102=$product=new SimpleXMLElement($result);在该行之前,执行var\u dump($result);退出();
并查看它显示的内容。