从CURL帖子返回php变量

时间:2017-03-01 01:35:37

标签: php json curl

我使用TradeGecko API并需要通过curl post请求创建新客户。它成功创建帖子并返回json响应(如下)。如何从JSON响应中检索id作为PHP变量?

function createCompany($client, $customerID, $customer_first_name, $customer_last_name, $customer_company_name,
                       $customer_company_email, $customer_company_phone)
{

    //API Url
    $url = 'https://api.tradegecko.com/companies';

    //Initiate cURL.
    $ch = curl_init($url);

    $jsonData =
        array(
            'assignee_id' => NULL,
            'default_ledger_account_id' => null,
            'default_payment_term_id' => null,
            'default_stock_location_id' => null,
            'default_tax_type_id' => null,
            'default_ledger_account_id' => null,
            'default_payment_term_id' => null,
            'default_stock_location_id' => null,
            'default_tax_type_id' => null,
            'company_code' => $customerID,
            'company_type' => "consumer",
            'default_discount_rate' => null,
            'default_price_list_id' => null,
            'default_tax_rate' => null,
            'default_document_theme_id' => null,
            'description' => "$customer_first_name $customer_last_name - synced for OceanAngler e-shop",
            'email' => "$customer_company_email",
            'fax' => null,
            'name' => "$customer_first_name $customer_last_name",
            'phone_number' => "$customer_company_phone",
            'status' => 'active',
            'tax_number' => null,
            'website' => null, // double check this in orders
            'default_price_type_id' => null


        );

    //Encode the array into JSON.
    $jsonDataEncoded = json_encode($jsonData);

    $authorization = "Authorization: Bearer [token goes here]";


    //Tell cURL that we want to send a POST request.
    curl_setopt($ch, CURLOPT_POST, 1);

    //Attach our encoded JSON string to the POST fields.
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

    //Set the content type to application/json
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));

    //Execute the request
    $result = curl_exec($ch);

}

它会返回这样的输出。

{  
  "company":{  
  "id":12697094,
  "created_at":"2017-03-01T01:28:28.679Z",
  "updated_at":"2017-03-01T01:28:28.679Z",
  "assignee_id":null,
  "default_ledger_account_id":null,
  "default_payment_term_id":null,
  "default_payment_method_id":null,
  "default_stock_location_id":null,
  "default_tax_type_id":null,
  "company_code":"12",
  "company_type":"consumer",
  "default_discount_rate":null,
  "default_price_list_id":null,
  "default_tax_rate":null,
  "default_document_theme_id":null,
  "description":"Aniken Skywalker - synced from e-shop",
  "email":"aniken@gmail.com",
  "fax":null,
  "name":"Aniken Skywalker",
  "phone_number":"0229329201",
  "status":"active",
  "tax_number":null,
  "website":null,
  "tags":null,
  "address_ids":[  

  ],
  "contact_ids":[  

  ],
  "note_ids":[  

  ],
  "default_price_type_id":null
  }
}

如何将'id'作为PHP变量返回?

2 个答案:

答案 0 :(得分:2)

问题在于curl请求,请在$ result中添加以下代码值。

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

之后:

$data = json_decode($result);

$id =  $data->company->id;

echo $id;

如有任何问题,请告诉我。

答案 1 :(得分:0)

我建议你查看这个问题How do I extract data from JSON with PHP?

顺便说一句,这是解决方案:

$data = json_decode($result);

$id =  $data->company->id;

echo $id;