Created
June 28, 2012 03:16
-
-
Save sugarknowledge/3008699 to your computer and use it in GitHub Desktop.
PHP Example using cURL with the v4 REST API to retrieve contacts and related email addresses with get_entry_list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$url = "http://{site_url}/service/v4/rest.php"; | |
$username = "admin"; | |
$password = "password"; | |
//function to make cURL request | |
function call($method, $parameters, $url) | |
{ | |
ob_start(); | |
$curl_request = curl_init(); | |
curl_setopt($curl_request, CURLOPT_URL, $url); | |
curl_setopt($curl_request, CURLOPT_POST, 1); | |
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | |
curl_setopt($curl_request, CURLOPT_HEADER, 1); | |
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0); | |
$jsonEncodedData = json_encode($parameters); | |
$post = array( | |
"method" => $method, | |
"input_type" => "JSON", | |
"response_type" => "JSON", | |
"rest_data" => $jsonEncodedData | |
); | |
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post); | |
$result = curl_exec($curl_request); | |
curl_close($curl_request); | |
$result = explode("\r\n\r\n", $result, 2); | |
$response = json_decode($result[1]); | |
ob_end_flush(); | |
return $response; | |
} | |
//login ------------------------------------------------------------------------------------------- | |
$login_parameters = array( | |
"user_auth"=>array( | |
"user_name"=>$username, | |
"password"=>md5($password), | |
"version"=>"1" | |
), | |
"application_name"=>"RestTest", | |
"name_value_list"=>array(), | |
); | |
$login_result = call("login", $login_parameters, $url); | |
/* | |
echo "<pre>"; | |
print_r($login_result); | |
echo "</pre>"; | |
*/ | |
//get session id | |
$session_id = $login_result->id; | |
//retrieve records ------------------------------------------------------------------------------- | |
$get_entry_list_parameters = array( | |
//session id | |
'session' => $session_id, | |
//The name of the module from which to retrieve records | |
'module_name' => "Contacts", | |
//The SQL WHERE clause without the word “where”. | |
'query' => "", | |
//The SQL ORDER BY clause without the phrase “order by”. | |
'order_by' => "", | |
//The record offset from which to start. | |
'offset' => "0", | |
//Optional. The list of fields to be returned in the results | |
'select_fields' => array( | |
'id', | |
'first_name', | |
'last_name', | |
), | |
//A list of link names and the fields to be returned for each link name | |
'link_name_to_fields_array' => array( | |
array( | |
'name' => 'email_addresses', | |
'value' => array( | |
'id', | |
'email_address', | |
'opt_out', | |
'primary_address' | |
), | |
), | |
), | |
//The maximum number of results to return. | |
'max_results' => '2', | |
//To exclude deleted records | |
'deleted' => 0, | |
//If only records marked as favorites should be returned. | |
'Favorites' => false, | |
); | |
$get_entry_list_result = call("get_entry_list", $get_entry_list_parameters, $url); | |
echo "<pre>"; | |
print_r($get_entry_list_result); | |
echo "</pre>"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stdClass Object | |
( | |
[result_count] => 2 | |
[total_count] => 200 | |
[next_offset] => 2 | |
[entry_list] => Array | |
( | |
[0] => stdClass Object | |
( | |
[id] => 116d9bc6-4a24-b826-952e-4f7cb6b25ea7 | |
[module_name] => Contacts | |
[name_value_list] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 116d9bc6-4a24-b826-952e-4f7cb6b25ea7 | |
) | |
[first_name] => stdClass Object | |
( | |
[name] => first_name | |
[value] => Lucinda | |
) | |
[last_name] => stdClass Object | |
( | |
[name] => last_name | |
[value] => Jacoby | |
) | |
) | |
) | |
[1] => stdClass Object | |
( | |
[id] => 11c263ef-ff61-71ff-f090-4f7cb6fc9f68 | |
[module_name] => Contacts | |
[name_value_list] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 11c263ef-ff61-71ff-f090-4f7cb6fc9f68 | |
) | |
[first_name] => stdClass Object | |
( | |
[name] => first_name | |
[value] => Ike | |
) | |
[last_name] => stdClass Object | |
( | |
[name] => last_name | |
[value] => Gassaway | |
) | |
) | |
) | |
) | |
[relationship_list] => Array | |
( | |
[0] => stdClass Object | |
( | |
[link_list] => Array | |
( | |
[0] => stdClass Object | |
( | |
[name] => email_addresses | |
[records] => Array | |
( | |
[0] => stdClass Object | |
( | |
[link_value] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 13066f13-d6ea-405c-0f95-4f7cb6fa3a08 | |
) | |
[email_address] => stdClass Object | |
( | |
[name] => email_address | |
[value] => [email protected] | |
) | |
[opt_out] => stdClass Object | |
( | |
[name] => opt_out | |
[value] => 0 | |
) | |
[primary_address] => stdClass Object | |
( | |
[name] => primary_address | |
[value] => | |
) | |
) | |
) | |
[1] => stdClass Object | |
( | |
[link_value] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 13e3d111-b226-c363-4832-4f7cb699a3a0 | |
) | |
[email_address] => stdClass Object | |
( | |
[name] => email_address | |
[value] => [email protected] | |
) | |
[opt_out] => stdClass Object | |
( | |
[name] => opt_out | |
[value] => 1 | |
) | |
[primary_address] => stdClass Object | |
( | |
[name] => primary_address | |
[value] => | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
[1] => stdClass Object | |
( | |
[link_list] => Array | |
( | |
[0] => stdClass Object | |
( | |
[name] => email_addresses | |
[records] => Array | |
( | |
[0] => stdClass Object | |
( | |
[link_value] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 16aeaf8b-b31f-943d-3844-4f7cb6ac63f2 | |
) | |
[email_address] => stdClass Object | |
( | |
[name] => email_address | |
[value] => [email protected] | |
) | |
[opt_out] => stdClass Object | |
( | |
[name] => opt_out | |
[value] => 0 | |
) | |
[primary_address] => stdClass Object | |
( | |
[name] => primary_address | |
[value] => | |
) | |
) | |
) | |
[1] => stdClass Object | |
( | |
[link_value] => stdClass Object | |
( | |
[id] => stdClass Object | |
( | |
[name] => id | |
[value] => 173bacf5-5ea6-3906-d91d-4f7cb6c6e883 | |
) | |
[email_address] => stdClass Object | |
( | |
[name] => email_address | |
[value] => [email protected] | |
) | |
[opt_out] => stdClass Object | |
( | |
[name] => opt_out | |
[value] => 1 | |
) | |
[primary_address] => stdClass Object | |
( | |
[name] => primary_address | |
[value] => | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment