Answers.Body: <p>When you are importing a large number of contacts, you could face timeout issues when using the user interface. Importing through CLI is an alternative. </p>
  
  <p>There is a file called import.php in /civicrm/civicrm/bin/csv/import.php. We will call this file from the command line.</p>
  
  <p>After you have exported your CSV file, here are the steps for the import:  </p>
  
  <ol>
  <li>Put the CSV file on your CiviCRM server, outside the webroot somewhere. (Do not place this file within the website root, or you risk exposing your contacts' data to the internet.)</li>
  <li>In the command prompt (shell), type <code>php path/to/import.php -e Contact --file path/to/file.csv</code> (check this <a href="https://forum.civicrm.org/index.php?topic=34033.0" rel="nofollow">forum post</a> for some more detail). Provide an absolute path to the CSV file.  </li>
  <li><p>At this point, depending on the contents of your CSV file, you will likely get an error such as <code>ERROR line 2: Mandatory key(s) missing from params array: contact_type</code>. It expects you to pass a contact_type parameter <strong>inside the CSV file</strong>. If you don't have one in your exported CSV file, you can very easily add a contact_type column in it using a Notepad++ macro (note: turn off autocompletion for words and quotes in Notepadd++ as it may screw up the macro). Just add the new column at the end. Here is an example of a CSV file:</p>
  
  <p>"email","contact_type"<br>
  "blabla@example.com","Individual"<br>
  "asdf@example.com","Individual"</p></li>
  <li><p>Now run the command again and you should see a success message for each imported contact. The message looks like this: <code>line 2: created Contact id: 114</code></p></li>
  </ol>
  
  <p>The down side is that you cannot (yet) assign a group or a tag to the imported contacts. I looked into the <a href="http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API" rel="nofollow">API</a> but could not see an ability to add contacts to groups or tags. 
  <br><br><br>
  Troubleshooting:</p>
  
  <ol>
  <li>If you get an error such as "cli.php can only be run from command line.", and you wonder about it because you ARE running from the command line, then likely you are using php as cgi/fcgi. To overcome this there is a little hack (which I am not too happy with, but we actually are using the code as intended here). In /bin/cli.class.php, on line 85 the function _accessing_from_cli() checks whether the program is run from CLI. It does this by checking the contents of PHP_SAPI. But according to the first comment <a href="http://php.net/manual/en/function.php-sapi-name.php" rel="nofollow">here</a>, that variable is not reliable in some cases. So we can modify the function like this:</li>
  </ol>
  
  <p><code>public function _accessing_from_cli() {
      if (PHP_SAPI === 'cli') {
        return TRUE;
      }
      else {
          /* Temporary hack since the PHP_SAPI check does not work correctly for cgi/fcgi. See first comment here: http://php.net/manual/en/function.php-sapi-name.php */
          return TRUE;
          //trigger_error("cli.php can only be run from command line.", E_USER_ERROR);
      }
    }</code></p>
  
  <p>After you import your contacts, do not forget to return the file back to its original version.</p>
  
  <ol start="2">
  <li>I really suggest you either configure php to show up errors or make sure you have access to the php error log. This way, if the import suddenly stops after some time, you'll be able to see the reason and take appropriate measures (increase memory_limit, max_execution_time, etc.).</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-08-14T11:11:43.703Z
Answers.LastActivityDate: 2016-10-27T03:14:02.027Z
Answers.LastEditDate: 2016-10-27T03:14:02.027Z
Answers.UserDisplayName: Borislav Zlatanov
Answers.Body: <p>Another option is to use <em>drush</em> in combination with the combination with the <em>cvapi</em> command. For example </p>
  
  <p><code>drush cvapi Contact.create sequential=1 contact_type="Individual" first_name="Ben" middle_name="Lee" last_name="User" </code> </p>
  
  <p>creates one contact. Specially useful when sometimes one contact must created by automatically by an unix script.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-10-25T20:16:18.473Z
Answers.LastActivityDate: 2016-10-25T20:16:18.473Z
Answers.UserDisplayName: Kainuk - CiviCooP
Body: <p>How can I import contacts in CiviCRM using the command line (CLI) rather than using the CiviCRM user interface and its importing wizard?</p>
  
Tags: <api><import><cli>
Title: How do I import contacts through the command line?
ThreadText: # How do I import contacts through the command line?
  
  (Asked by: username_0 on 2015-08-14)
  
  How can I import contacts in CiviCRM using the command line (CLI) rather than using the CiviCRM user interface and its importing wizard?
  
  **Tags:** api, import, cli
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2015-08-14. Score: 13
  
  When you are importing a large number of contacts, you could face timeout issues when using the user interface. Importing through CLI is an alternative.
  
  There is a file called import.php in /civicrm/civicrm/bin/csv/import.php. We will call this file from the command line.
  
  After you have exported your CSV file, here are the steps for the import:
  
  1. Put the CSV file on your CiviCRM server, outside the webroot somewhere. (Do not place this file within the website root, or you risk exposing your contacts' data to the internet.)
  2. In the command prompt (shell), type `php path/to/import.php -e Contact --file path/to/file.csv` (check this [forum post](https://forum.civicrm.org/index.php?topic=34033.0) for some more detail). Provide an absolute path to the CSV file.
  3. At this point, depending on the contents of your CSV file, you will likely get an error such as `ERROR line 2: Mandatory key(s) missing from params array: contact_type`. It expects you to pass a contact\_type parameter **inside the CSV file**. If you don't have one in your exported CSV file, you can very easily add a contact\_type column in it using a Notepad++ macro (note: turn off autocompletion for words and quotes in Notepadd++ as it may screw up the macro). Just add the new column at the end. Here is an example of a CSV file:
  
     "email","contact\_type"  
     "blabla@example.com","Individual"  
     "asdf@example.com","Individual"
  4. Now run the command again and you should see a success message for each imported contact. The message looks like this: `line 2: created Contact id: 114`
  
  The down side is that you cannot (yet) assign a group or a tag to the imported contacts. I looked into the [API](http://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API) but could not see an ability to add contacts to groups or tags.
    
    
    
  Troubleshooting:
  
  1. If you get an error such as "cli.php can only be run from command line.", and you wonder about it because you ARE running from the command line, then likely you are using php as cgi/fcgi. To overcome this there is a little hack (which I am not too happy with, but we actually are using the code as intended here). In /bin/cli.class.php, on line 85 the function \_accessing\_from\_cli() checks whether the program is run from CLI. It does this by checking the contents of PHP\_SAPI. But according to the first comment [here](http://php.net/manual/en/function.php-sapi-name.php), that variable is not reliable in some cases. So we can modify the function like this:
  
  `public function _accessing_from_cli() {
  if (PHP_SAPI === 'cli') {
  return TRUE;
  }
  else {
  /* Temporary hack since the PHP_SAPI check does not work correctly for cgi/fcgi. See first comment here: http://php.net/manual/en/function.php-sapi-name.php */
  return TRUE;
  //trigger_error("cli.php can only be run from command line.", E_USER_ERROR);
  }
  }`
  
  After you import your contacts, do not forget to return the file back to its original version.
  
  2. I really suggest you either configure php to show up errors or make sure you have access to the php error log. This way, if the import suddenly stops after some time, you'll be able to see the reason and take appropriate measures (increase memory\_limit, max\_execution\_time, etc.).
  
  
  ---
  
  ### Answer by username_1 on 2016-10-25. Score: 3
  
  Another option is to use *drush* in combination with the combination with the *cvapi* command. For example
  
  `drush cvapi Contact.create sequential=1 contact_type="Individual" first_name="Ben" middle_name="Lee" last_name="User"`
  
  creates one contact. Specially useful when sometimes one contact must created by automatically by an unix script.
  
  
  ---
  
  