How do I set the value of external_id to be the same as a custom field?
The Contact.get API will return custom fields if requested:

&lt;?php

$contacts = civicrm_api3('Contact', 'get', array(
  'sequential' =&gt; 1,
  'return' =&gt; array('custom_32'),
  'options' =&gt; array('limit' =&gt; 0),
));

foreach ($contacts['values'] as $contact) {
  $res = civicrm_api3('Contact', 'create', array(
    'id' =&gt; $contact['id'],  
    'external_identifier' =&gt; $contact['custom_32'],
  ));
}


You might want more parameters on the Contact.get (eg 'contact_is_deleted' => 0) and some error checking on the Contact.create.  As noted in previous comments, there are restrictions around external id's which the custom field may not comply with.

If there are many contacts, you may need to change 'limit' and do it in chunks.

You can save the above to a file, and use cv to run it:

cv scr /path/to/file