1

我使用 magento 中的 zend http 客户端获取 json 文件内容。我不需要 getBody() 上的标题信息。我确实在它工作的一些主机上对其进行了测试。但是现场主持人给我带来了麻烦。

$request_url = "link";
$httpClientConfig = array('maxredirects' => 0);
$client = new Zend_Http_Client($request_url, $httpClientConfig);
$client->setMethod(Zend_Http_Client::GET);
try {
    $response = $client->request();
} catch (Exception $e) {
    Mage::throwException($this->__('Gateway request error: %s', $e->getMessage()));
}

Mage::log($response->getBody());

结果记录:

2015-01-08T09:12:46+00:00 DEBUG (7): HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 761
Connection: close
Date: Mon, 05 Jan 2015 22:53:40 GMT
Last-Modified: Sat, 15 Nov 2014 10:14:17 GMT
ETag: "a8b17a42b7ef7e5960f9bd325a8c1892"
Accept-Ranges: bytes
Server: AmazonS3
Age: 5574
X-Cache: Hit from cloudfront
Via: 1.1 522dd06c4c8acf822ccbebe21aee8d1c.cloudfront.net (CloudFront)
X-Amz-Cf-Id: c6K9QJnOESg1NERKjG-v2fX_9eskmCzz_KUYdXrOb2NSjVTbWZ_x8Q==
{
  "assets": {
    "standard": {
      "url": "https://d3k1w8lx8mqizo.cloudfront.net/standard.png",
    "infobox": {
        "page1": "https://d3k1w8lx8mqizo.cloudfront.net/frontside.png",
        "page2": "https://d3k1w8lx8mqizo.cloudfront.net/backside.png"
      }
    },
    "promotion": {
      "infobox": {
        "page1": "https://d3k1w8lx8mqizo.cloudfront.net/frontside.png",
        "page2": "https://d3k1w8lx8mqizo.cloudfront.net/backside.png"
      },
      "url": "https://d3k1w8lx8mqizo.cloudfront.net/standard.png",
    "interest_free_months": 6,
    "transaction_limit_min": 240.0
    }
  }
}
4

1 回答 1

1

试试这个:

Zend_Http_Response::extractBody($response->getBody());

或者在实例化 Zend_Http_Client 时不要检索请求中的标头:

$httpClientConfig = array(
    'maxredirects' => 0, 
    'curloptions' => array(CURLOPT_HEADER => false),
);

如果上述方法不起作用,您可以尝试另一种方法来执行请求,例如 file_get_contents:

$response = file_get_contents($request_url);

我希望它有所帮助。

于 2015-01-08T16:23:31.793 回答