背景: 我正在编写一段PHP代码,它使用RESTful API连接到另一方。 我正在使用Laravel 4.2,PHP5.5,并使用Guzzle作为客户端。
目前我正在尝试通过API获取一些数据,这会返回一个XML对象。
代码:
public function getCategories()
{
$client = new Guzzle();
$req = $client->createRequest('get', $this->api_url."cateservice/category", [
'headers'=> [
'Content-type' => 'application/xml',
'Accept-charset' => 'utf-8',
'openapikey' => $this->api_key
]
]);
$xml = $client->send($req)->getBody();
}
如果我要运行simplexml_load_string($xml)
,它将返回一个完全空的对象。如果我使用Guzzle的默认->xml()
函数,也一样。
使用浏览器,我设法得到了支持的响应主体:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:categorys
xmlns:ns2="http://skt.tmall.business.openapi.spring.service.client.domain">
<ns2:category>
<depth>2</depth>
<dispEngNm>test middle2</dispEngNm>
<dispNm>test middle2</dispNm>
<dispNo>1642</dispNo>
<parentDispNo>1639</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>test small2</dispEngNm>
<dispNm>test small2</dispNm>
<dispNo>1643</dispNo>
<parentDispNo>1642</parentDispNo>
</ns2:category>
<ns2:category>
<depth>1</depth>
<dispEngNm>Women Clothing</dispEngNm>
<dispNm>Women Clothing</dispNm>
<dispNo>2021</dispNo>
<parentDispNo>0</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Tops</dispEngNm>
<dispNm>Tops</dispNm>
<dispNo>2051</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Sleeveless</dispEngNm>
<dispNm>Sleeveless</dispNm>
<dispNo>2209</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Short Sleeve</dispEngNm>
<dispNm>Short Sleeve</dispNm>
<dispNo>2210</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>3/4 Sleeve</dispEngNm>
<dispNm>3/4 Sleeve</dispNm>
<dispNo>2211</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Long Sleeve</dispEngNm>
<dispNm>Long Sleeve</dispNm>
<dispNo>2212</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>T-shirts</dispEngNm>
<dispNm>T-shirts</dispNm>
<dispNo>2214</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Shirts</dispEngNm>
<dispNm>Shirts</dispNm>
<dispNo>2215</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Blouse</dispEngNm>
<dispNm>Blouse</dispNm>
<dispNo>2216</dispNo>
<parentDispNo>2051</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Dresses</dispEngNm>
<dispNm>Dresses</dispNm>
<dispNo>2052</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Mini</dispEngNm>
<dispNm>Mini</dispNm>
<dispNo>2217</dispNo>
<parentDispNo>2052</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Midi</dispEngNm>
<dispNm>Midi</dispNm>
<dispNo>2218</dispNo>
<parentDispNo>2052</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Maxi</dispEngNm>
<dispNm>Maxi</dispNm>
<dispNo>2219</dispNo>
<parentDispNo>2052</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Traditional wear</dispEngNm>
<dispNm>Traditional wear</dispNm>
<dispNo>2053</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Kurti</dispEngNm>
<dispNm>Kurti</dispNm>
<dispNo>2221</dispNo>
<parentDispNo>2053</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Cheongsam</dispEngNm>
<dispNm>Cheongsam</dispNm>
<dispNo>2222</dispNo>
<parentDispNo>2053</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Skirts</dispEngNm>
<dispNm>Skirts</dispNm>
<dispNo>2054</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Mini</dispEngNm>
<dispNm>Mini</dispNm>
<dispNo>2223</dispNo>
<parentDispNo>2054</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Midi</dispEngNm>
<dispNm>Midi</dispNm>
<dispNo>2224</dispNo>
<parentDispNo>2054</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Maxi</dispEngNm>
<dispNm>Maxi</dispNm>
<dispNo>2225</dispNo>
<parentDispNo>2054</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Pants, Leggings & Shorts</dispEngNm>
<dispNm>Pants, Leggings & Shorts</dispNm>
<dispNo>2055</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Pants</dispEngNm>
<dispNm>Pants</dispNm>
<dispNo>2227</dispNo>
<parentDispNo>2055</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Denim</dispEngNm>
<dispNm>Denim</dispNm>
<dispNo>2228</dispNo>
<parentDispNo>2055</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Leggings & Tights</dispEngNm>
<dispNm>Leggings & Tights</dispNm>
<dispNo>2229</dispNo>
<parentDispNo>2055</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Shorts</dispEngNm>
<dispNm>Shorts</dispNm>
<dispNo>2230</dispNo>
<parentDispNo>2055</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Jumpsuits & Playsuits</dispEngNm>
<dispNm>Jumpsuits & Playsuits</dispNm>
<dispNo>2056</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Jumpsuits</dispEngNm>
<dispNm>Jumpsuits</dispNm>
<dispNo>2231</dispNo>
<parentDispNo>2056</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Playsuits</dispEngNm>
<dispNm>Playsuits</dispNm>
<dispNo>2232</dispNo>
<parentDispNo>2056</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Outerwear</dispEngNm>
<dispNm>Outerwear</dispNm>
<dispNo>2057</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Cardigans</dispEngNm>
<dispNm>Cardigans</dispNm>
<dispNo>2233</dispNo>
<parentDispNo>2057</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Sweaters</dispEngNm>
<dispNm>Sweaters</dispNm>
<dispNo>2234</dispNo>
<parentDispNo>2057</parentDispNo>
</ns2:category>
<ns2:category>
<depth>3</depth>
<dispEngNm>Blazers & Coats</dispEngNm>
<dispNm>Blazers & Coats</dispNm>
<dispNo>2235</dispNo>
<parentDispNo>2057</parentDispNo>
</ns2:category>
<ns2:category>
<depth>2</depth>
<dispEngNm>Muslimah wear</dispEngNm>
<dispNm>Muslimah wear</dispNm>
<dispNo>2058</dispNo>
<parentDispNo>2021</parentDispNo>
</ns2:category>
问题: 我如何解析这段XML,使它成为一个数组,一个PHP对象,或任何允许我访问/循环这些“类别”的东西?
编辑:我正在尝试遍历这些类别,以便我可以处理并将它们存储到我的数据库中。当所有函数返回空对象或空数组时,我对如何处理这个问题一无所知。