Laravel GuzzleHTTP对JSON的响应

时间:2016-09-05 12:27:56

标签: php json xml laravel guzzle

我正在调用NameCheap的API并返回XML响应。

尝试输出时,我得到的响应为NULL。

使用相同的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);

    }

我回来了

null

3 个答案:

答案 0 :(得分:4)

这就是我用来做的事情......

从请求开始,如下所示:

$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);

然后转换您的回复,例如:

// json
$json = json_encode($xml);

// array
$array = json_decode($json, true);

// array - dot notation
$array_dot = array_dot($array);

// collection
$collection = collect($array);

答案 1 :(得分:0)

你说它们返回XML,但是你试图将它解析为JSON,它将变为null。

将正文保存在变量和smtps中,而不是将其发送到dd()以确认是否为XML并且您正在收到回复。

之后考虑使用像SimpleXml http://php.net/manual/en/book.simplexml.php

这样的XML解析器

答案 2 :(得分:0)

试试这个

$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();
相关问题