JSON响应没有保存到php变量中

时间:2015-08-10 10:18:23

标签: php json curl

我很惊讶地看到这一点,因为我多次使用CURL并且之前从未得到过这个问题所以这个问题对你来说可能看起来很愚蠢,但我遇到了这个问题,需要解决它。

我正在对Jet API进行“GET”调用,并将响应直接抛到页面中,如下所示

{
  "buyer": {
    "name": "Snowball",
    "phone_number": "212-212-8739"
  },
  "fulfillment_node": "328c3fdb7909495a8f343fcf2ed4b649",
  "has_shipments": false,
  "jet_request_directed_cancel": false,
  "merchant_order_id": "eef1b6bcf2fb460b80ba1642587c4af6",
  "order_detail": {
    "request_shipping_carrier": "USPS",
    "request_service_level": "Standard",
    "request_ship_by": "2015-07-31T10:46:13.9045615Z",
    "request_delivery_by": "2015-08-01T10:46:13.9045615Z"
  },
  "order_items": [
    {
      "order_item_id": "80a49174d35346dc843b0c41f8cce6cd",
      "merchant_sku": "0121_14KW-11-1HSI1",
      "request_order_quantity": 2,
      "request_order_cancel_qty": 0,
      "item_tax_code": "",
      "item_price": {
        "item_tax": null,
        "item_shipping_cost": 0.90,
        "item_shipping_tax": null,
        "base_price": 29.9
      },
      "product_title": "test1234",
      "url": "http://www.jet.com/api/merchant_skus/0121_14KW-11-1HSI1"
    }
  ],
  "order_placed_date": "2015-07-30T10:45:13.9045615Z",
  "order_totals": {
    "item_price": {
      "item_tax": null,
      "item_shipping_cost": 1.80,
      "item_shipping_tax": null,
      "base_price": 59.8
    }
  },
  "order_transmission_date": "2015-07-30T10:46:13.9045615Z",
  "reference_order_id": "292301679002",
  "shipping_to": {
    "recipient": {
      "name": "Homer Simpson",
      "phone_number": "212-212-2307"
    },
    "address": {
      "address1": "77 Oak street",
      "address2": "",
      "city": "Old Cairo",
      "state": "MS",
      "zip_code": "38829"
    }
  },
  "status": "ready"
}CORRECT<pre>1</pre>

我的脚本代码:

function curlWrap($url, $json, $action, $headers)
    {
        $ch         =           curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        switch($action){
            case "POST":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                break;
            case "GET":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
                break;
            case "PUT":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                break;
            case "DELETE":
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
                break;
            default:
                break;
            }

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            //curl_setopt($ch, CURLINFO_HEADER_OUT, true);

            $output = curl_exec($ch);

            echo "CORRECT";
            echo "<pre>";   print_r($output);   echo "</pre>";              

            echo "WRONG";

            //$info     = curl_getinfo($ch);

            //echo "<pre>"; print_r($info); echo "</pre>";

            curl_close($ch);
            $decoded = json_decode($output, true);
            return $decoded;
    }

如果您看到我的代码,来自API服务器的JSON响应应该保存到$output变量中,但是当我查看页面的来源时,响应会出现在页面顶部。

期待您的帮助。

1 个答案:

答案 0 :(得分:1)

您可能执行了错误的复制/粘贴操作,忘记在{:1>}中替换Global.asax

$chlead

如果您在开发环境中激活了警告,则可能会看到curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true); curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0); 未声明或chlead无法设置选项的内容。

如果没有curl_setoptCURLOPT_RETURNTRANSFER,则true的行为会直接输出。