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


JSON response is not saving into php variable

我很惊讶地看到这个,因为我已经使用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>

My Script Code:

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;
    }

如果你看到我的代码,JSON响应来自API服务器应该保存到$output变量,但当我查看页面的源,响应来到页面的顶部。

期待您的帮助。

你可能做了一个糟糕的复制/粘贴,忘记替换$chlead:

curl_setopt($chlead, CURLOPT_RETURNTRANSFER, true);
curl_setopt($chlead, CURLOPT_SSL_VERIFYPEER, 0);

如果您在开发环境中激活了警告,您可能已经看到有关chlead未被声明或curl_setopt无法设置选项的内容。

没有CURLOPT_RETURNTRANSFERtrue, curl_exec的行为是直接输出