Skip to main content
let's NOT advise people to put CSV of contact data in the webroot ;)
Source Link
Chris Burgess
  • 9.9k
  • 15
  • 45
  1. Put the CSV file in your site's root directory (or other convenient place).

    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 for some more detail). Provide an absolute path to the CSV file.

    In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post 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:

    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"
    "[email protected]","Individual"
    "[email protected]","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

"email","contact_type"
"[email protected]","Individual"
"[email protected]","Individual"

  1. 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
  1. Put the CSV file in your site's root directory (or other convenient place).
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post 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"
"[email protected]","Individual"
"[email protected]","Individual"

  1. 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
  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 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"
    "[email protected]","Individual"
    "[email protected]","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

Added one more troubleshooting tip.
Source Link

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, that variable is not reliable in some cases. So we can modify the function like this:

  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, that variable is not reliable in some cases. So we can modify the function like this:
  1. 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.).

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, that variable is not reliable in some cases. So we can modify the function like this:

  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, that variable is not reliable in some cases. So we can modify the function like this:
  1. 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.).
improved formatting
Source Link

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 in your site's root directory (or other convenient place).
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post 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"
"[email protected]","Individual"
"[email protected]","Individual"

  1. 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 but could not see an ability to add contacts to groups or tags.


Troubleshooting:

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, 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.

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 in your site's root directory (or other convenient place).
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post 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"
"[email protected]","Individual"
"[email protected]","Individual"

  1. 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 but could not see an ability to add contacts to groups or tags.


Troubleshooting:

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, 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.

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 in your site's root directory (or other convenient place).
  2. In the command prompt (shell), type php path/to/import.php -e Contact --file path/to/file.csv (check this forum post 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"
"[email protected]","Individual"
"[email protected]","Individual"

  1. 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 but could not see an ability to add contacts to groups or tags.


Troubleshooting:

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, 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.

Source Link
Loading