dongyuxin5353
2015-08-10 10:18
浏览 51

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

I am very surprised to see this as i have been using CURL many times and never got this before so this question may look stupid to you but i am having this issue and need to fix it.

I am making a "GET" call to Jet API and it throws the response directly into page, given below

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

If you see my code, JSON response coming from API server supposed to be saved into $output variable but when I view source of the page, response coming on top of the page.

Looking forward to your help.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duandou9931 2015-08-10 13:09
    已采纳

    You probably did a bad copy/paste and forgot to replace $chlead in:

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

    If you had warnings activated on your development environment you could have seen something about chlead not being declared or curl_setopt not able to set the option.

    Without CURLOPT_RETURNTRANSFER to true, the behavior of curl_exec is outputting it out directly.

    打赏 评论

相关推荐 更多相似问题