Answers.Body: <p>you need to replace the url parameters <strong>api_key</strong> and <strong>key</strong> with the relevant keys. 
  More information can be found in the <a href="https://docs.civicrm.org/dev/en/latest/api/interfaces/#rest" rel="nofollow noreferrer">developer documents</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-01T21:15:28.137Z
Answers.Text: in addition to @pradeep-nayak answer
Answers.UserDisplayName: Chamil Wijesooriya
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-02T14:41:33.433Z
Answers.Text: Yes I have replaced the keys in my actual code. Not posting them here. THanks
Answers.UserDisplayName: Scott Pulver
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-01T21:09:44.310Z
Answers.LastActivityDate: 2018-07-01T21:09:44.310Z
Answers.UserDisplayName: Chamil Wijesooriya
Answers.Body: <p>I use CURL to call rest api. Here is the snippet you can ignore/remove many lines that sets curl variable.</p>
  
  <pre><code>$url = 'http://domain.org/sites/all/modules/civicrm/extern/rest.php?entity=Contact&amp;action=getsingle&amp;api_key=HR76BS710C&amp;key=8c1cf9913fd674e08c8f27804334eb5a&amp;json={"id":1}';
  
  $ch = curl_init();
  //http://php.net/manual/en/function.curl-setopt.php
  //$ch = curl_init($url);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  
  //for debugging?
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  
  $data = curl_exec($ch);
  curl_close($ch);
  $obj = json_decode($data);
  </code></pre>
  
  <p>HTH</p>
  
  <p>Pradeep</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-07-01T21:05:28.790Z
Answers.LastActivityDate: 2018-07-01T21:05:28.790Z
Answers.UserDisplayName: Pradeep Nayak
Body: <p>Been trying to get this to work for hours. Help would be greatly appreciated. userkey and sitekey are replaced with actual keys.</p>
  
  <p>I'm trying to run in a php file:</p>
  
  <pre><code>$result = curl -X POST "https://www.mysite.org/sites/all/modules/civicrm/extern/rest.php?entity=Membership&amp;action=get&amp;api_key=userkey&amp;key=sitekey&amp;json={"sequential":1,"return":"membership_type_id,id,end_date","contact_id":2,"active_only":1,"options":{"limit":1,"sort":"join_date DESC"}}";
  </code></pre>
  
  <p>I get the error:</p>
  
  <pre><code>PHP Parse error:  syntax error, unexpected 'POST' (T_STRING) in /home/spulver/civicrmapi.php on line 3
  </code></pre>
  
  <p>Any ideas how to fix?</p>
  
Tags: <civimember><api><php>
Title: How to use CiviCRM REST API in external PHP code
ThreadText: # How to use CiviCRM REST API in external PHP code
  
  (Asked by: username_0 on 2018-07-01)
  
  Been trying to get this to work for hours. Help would be greatly appreciated. userkey and sitekey are replaced with actual keys.
  I'm trying to run in a php file:
  $result = curl -X POST "https://www.mysite.org/sites/all/modules/civicrm/extern/rest.php?entity=Membership&action=get&api_key=userkey&key=sitekey&json={"sequential":1,"return":"membership_type_id,id,end_date","contact_id":2,"active_only":1,"options":{"limit":1,"sort":"join_date DESC"}}";
  
  I get the error:
  PHP Parse error:  syntax error, unexpected 'POST' (T_STRING) in /home/spulver/civicrmapi.php on line 3
  
  Any ideas how to fix?
  
  **Tags:** civimember, api, php
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2018-07-01. Score: 3 | Accepted answer
  
  I use CURL to call rest api. Here is the snippet you can ignore/remove many lines that sets curl variable.
  $url = 'http://domain.org/sites/all/modules/civicrm/extern/rest.php?entity=Contact&action=getsingle&api_key=HR76BS710C&key=8c1cf9913fd674e08c8f27804334eb5a&json={"id":1}';
  
  $ch = curl_init();
  //http://php.net/manual/en/function.curl-setopt.php
  //$ch = curl_init($url);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  //curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_ENCODING, "");
  
  //for debugging?
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  
  $data = curl_exec($ch);
  curl_close($ch);
  $obj = json_decode($data);
  
  HTH
  Pradeep
  
  
  ---
  
  ### Answer by username_1 on 2018-07-01. Score: 0
  
  you need to replace the url parameters **api\_key** and **key** with the relevant keys.
  More information can be found in the [developer documents](https://docs.civicrm.org/dev/en/latest/api/interfaces/#rest).
  
  > **Comments:**
  > **username_1**: in addition to @pradeep-nayak answer  
  > **username_0**: Yes I have replaced the keys in my actual code. Not posting them here. THanks  
  
  ---
  
  