Laurie Williams Laurie Williams -4 years ago 143
JSON Question

Return php variable from CURL post

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?

Answer Source

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 .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download