[英]Curl Response to php object
I making this curl request: 我提出这个卷曲请求:
$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"]);
The response is this: 答案是这样的:
{"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}
The log statement prints nothing. 日志语句不打印任何内容。 What I am doing wrong here? 我在这做错了什么?
json_decode
decodes to an object by default. json_decode
默认解码为对象。 Do $jsonData = json_decode($response, true);
do $jsonData = json_decode($response, true);
if you want an associative array. 如果你想要一个关联数组。
Mage::log( $jsonData->productDetails[0]->product);
或者如上所述使用关联数组。
Use curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
使用curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
in your curl and pass true
with json_decode()
.... 在你的curl中并使用json_decode()
传递true
$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']);
This will output : 这将输出:
Philips QT4000 Trimmer Black
Here is how you make a curl call and get the content from that website 以下是您如何拨打电话并从该网站获取内容
<?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>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.