我发出这个卷曲请求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "affiliate-feeds.snapdeal.com/feed/api/order?startDate=2016-01-01&endDate=2016-05-03&status=approved");
curl_setopt(
$ch, CURLOPT_HTTPHEADER,
array(
'Snapdeal-Affiliate-Id:'.$affiliateId,
'Snapdeal-Token-Id:'.$token,
'Accept:application/json'
)
);
$response = curl_exec($ch);
curl_close($ch);
// work with $response here:
$jsonData = json_decode($response);
Mage::log( $jsonData["productDetails"][0]["product"]);
答案如下:
{"productDetails":[{"product":"Philips QT4000 Trimmer Black","category":"Appliances","orderCode":"12569696012","quantity":1,"price":936.0,"sale":936.0,"commissionRate":1.0,"commissionEarned":9.36,"dateTime":"03/29/2016 22:49:06","affiliateSubId1":"","affiliateSubId2":"null","userType":"EXISTING","deviceType":"web"}],"nextURL":null}
日志语句不打印任何内容。我在这里做错了什么?
答案 0 :(得分:2)
json_decode
解码为对象。如果你想要一个关联数组,请$jsonData = json_decode($response, true);
。
答案 1 :(得分:1)
Mage::log( $jsonData->productDetails[0]->product);
或者如上所述使用关联数组。
答案 2 :(得分:1)
在你的卷曲中使用curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
并将true
与json_decode()
.... {/ p>一起传递
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "affiliate-feeds.snapdeal.com/feed/api/order?startDate=2016-01-01&endDate=2016-05-03&status=approved");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt(
$ch, CURLOPT_HTTPHEADER,
array(
'Snapdeal-Affiliate-Id:'.$affiliateId,
'Snapdeal-Token-Id:'.$token,
'Accept:application/json'
)
);
$response = curl_exec($ch);
curl_close($ch);
// work with $response here:
$jsonData = json_decode($response,true);
Mage::log($jsonData['productDetails'][0]['product']);
这将输出:
Philips QT4000 Trimmer Black
答案 3 :(得分:-1)
以下是您进行curl呼叫并从该网站获取内容的方法
<?php
if (!function_exists('curl_version')) {
exit("Enable cURL in PHP");
}
$url = "https://www.google.com/";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Host: " . url($url),
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36",
"accept-encoding: gzip, deflate",
"cache-control: no-cache",
),
));
function url($url)
{
$result = parse_url($url);
return $result['host'];
}
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo "<textarea>" . $response . "</textarea>";
}