【问题标题】:Curl Response to php object对 php 对象的卷曲响应
【发布时间】:2016-05-04 10:25:23
【问题描述】:

我提出这个 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(
                    '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}

日志语句不打印任何内容。我在这里做错了什么?

【问题讨论】:

标签: php curl


【解决方案1】:

json_decode 默认解码为对象。如果需要关联数组,请执行 $jsonData = json_decode($response, true);

【讨论】:

  • 我做了那个改变,它仍然没有给我任何东西。你能写完整的语句在日志中使用吗
  • @mayank 您无需将调用中的语句更改为log。我刚刚使用您提供的 JSON 对此进行了测试,并且效果很好。您确定您正在为日志输出查找正确的文件吗?
  • 您是直接使用 json 的,可能就是这个问题。也许,获取 curl 响应然后在其上使用 json_decode 似乎是个麻烦。也许我必须将 curl 响应转换为某种东西
【解决方案2】:

在你的 curl 中使用 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 并通过 truejson_decode()....

    $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

【讨论】:

  • 我做了那个改变:这里是:$response = curl_exec($ch); curl_close($ch); // 在这里使用 $response: $jsonData = json_decode($response,true); Mage::log($jsonData["productDetails"][0]["product"],null,"mylogfile1.log",true);仍然不打印任何内容
  • 在日志中使用 $array['productDetails'][0]['product'] 而不是 $jsonData->productDetails[0]->product..
  • json_decode($json,true) 返回关联数组,关联数组以键值形式打印,例如 $array['key'] 而不是 $array->key
  • 只需使用echo $jsonData['productDetails'][0]['product'] 并查看打印的内容...
  • 打印完整的 json 响应。我们要开始聊天吗?
【解决方案3】:
Mage::log( $jsonData->productDetails[0]->product); 

或者使用前面提到的关联数组。

【讨论】:

  • 仍然不打印任何东西
  • @mayank 当你说它“不打印任何东西”时,你是什么意思?它是一种日志方法,因此它很可能会记录到文件而不是产生输出。如果要输出,请使用echo
  • 抱歉措辞错误,它什么也不记录,(一个空值)
  • 使用 var_dump($jsonData->productDetails[0]->product)
【解决方案4】:

这是你如何进行 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>";

}

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多