2

I working with the TradeGecko API and need to create a new customer via a curl post request. It creates the post successfully and returns a json response (below). How do I retrieve the id from the JSON response as a PHP variable?

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

}

It returns an output like this.

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

How do I return 'id' as a PHP variable?

2 Answers2

3

The problem is with curl request, please add below code for value in $result.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

after that :

$data = json_decode($result);

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

echo $id;

Let me know in case any problem .

Vijay
  • 246
  • 1
  • 6
1

I suggest you to check this question How do I extract data from JSON with PHP?

By the way here is the solution :

$data = json_decode($result);

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

echo $id;
Community
  • 1
  • 1
Faxsy
  • 351
  • 1
  • 4
  • 17
  • Thank you. I don't think `$result` contains any JSON to decode. When I `print_r($data)`, nothing is output. –  Mar 01 '17 at 01:59
  • You wrote in your question that your result returns json – Faxsy Mar 01 '17 at 03:19