对php对象的Curl响应
我提出这一要求:对php对象的Curl响应,php,curl,Php,Curl,我提出这一要求: $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
$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}
log语句不打印任何内容。我做错了什么?默认情况下解码为对象。Do$jsonData=json\u解码($response,true)代码>如果需要关联数组
Mage::log( $jsonData->productDetails[0]->product);
或者使用前面提到的关联数组。使用curl\u setopt($ch,CURLOPT\u RETURNTRANSFER,true)使用json\u 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']);
这将输出:
Philips QT4000 Trimmer Black
以下是您如何拨打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>";
}
可能与此重复,使用json\u decode($response,true)
它将返回一个关联数组…我做了更改,但仍然没有给我任何信息。你能写出要在里面使用的完整语句吗log@mayank您不需要更改对log
的调用中的语句。我刚刚用您提供的JSON对其进行了测试,效果很好。您确定要在正确的文件中查找日志输出吗?您正在直接使用json,也许这就是问题所在。也许,获取curl响应,然后在其上使用json_解码似乎是个麻烦。也许我必须把卷曲的反应转化成一些仍然无法打印的东西anything@mayank当你说它“不打印任何东西”你是什么意思?它是一种日志方法,因此最有可能是将日志记录到文件,而不是生成输出。如果您想要输出,请使用echo
。很抱歉,它没有记录任何内容(一个空值)使用var\u dump($jsonData->productDetails[0]->product)我做了更改:这里是:$response=curl\u exec($ch);卷曲关闭($ch);//在这里使用$response:$jsonData=json\u decode($response,true);Mage::log($jsonData[“productDetails”][0][“product”],null,“mylogfile1.log”,true);仍然不打印任何语言$array['productDetails'][0]['product']
而不是日志中的$jsonData->productDetails[0]->product
。json\u decode($json,true)
返回关联数组,关联数组使用键对值打印,如$array['key']
而不是$array->key
只需使用echo$jsonData['productDetails'][0]['product']
并查看打印的内容…它打印完整的json响应。我们可以开始聊天吗?