[英]Laravel GuzzleHTTP XML Response to JSON
I'm making a call the NameCheap's API and they return a XML response. 我正在调用NameCheap的API,并且它们返回XML响应。
When trying to output this, I get the response NULL. 尝试输出此内容时,我得到响应NULL。
Hitting the same API but with the Google Extension POSTMAN I get the results I'm after, am I doing something wrong with the response? 使用相同的API,但使用Google Extension POSTMAN,我得到了我想要的结果,响应做错了吗?
public function testCheck($domains){
$client = new Client();
$res = $client->request('GET', 'https://api.namecheap.com/xml.response?ApiUser=(username)&ApiKey=(apikey)&UserName(username)&ClientIp=(ip)&Command=namecheap.domains.check&DomainList=' . $domains);
$data = json_decode($res->getBody());
dd($data);
}
I get back 我回来
null
This is what I'm using to do just that... 这就是我用来做的...
Start with the request, like this: 从请求开始,像这样:
$client = new Client;
$results = $client->request('GET', $this->namecheap_sandbox_url, [
'query' => [
'ApiUser' => $this->ApiUser,
'ApiKey' => $this->SandboxApiKey,
'UserName' => $this->UserName,
'Command' => $this->CheckCommand,
'ClientIp' => $this->ClientIp,
'DomainList' => $this->DomainList
]
]);
$xml = simplexml_load_string($results->getBody(),'SimpleXMLElement',LIBXML_NOCDATA);
Then convert your response, for example: 然后转换您的响应,例如:
// json
$json = json_encode($xml);
// array
$array = json_decode($json, true);
// array - dot notation
$array_dot = array_dot($array);
// collection
$collection = collect($array);
You say they return XML, but you are trying to parse it as JSON, that will get null. 您说他们返回XML,但是您尝试将其解析为JSON,它将得到null。
Save the body in a variable and dd()
that instead of sending it to json_decode()
to confirm is XML and that you are getting the response. 将正文保存在变量和
dd()
,而不是将其发送到json_decode()
来确认是XML,并且您正在获取响应。
After that consider using an XML parser like SimpleXml http://php.net/manual/en/book.simplexml.php 之后,请考虑使用XML解析器,例如SimpleXml http://php.net/manual/zh/book.simplexml.php
尝试这个
$res = $client->request('GET', 'https://api.namecheap.com/xml.response?ApiUser=(username)&ApiKey=(apikey)&UserName(username)&ClientIp=(ip)&Command=namecheap.domains.check&DomainList=' . $domains)->send()->json();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.