Contact Image URL is directly stored in civicrm_contact
table, so I think if you have the URL which can display an image, you should be directly able to use the contact create API as below -
$result = civicrm_api3('Contact', 'create', array(
'contact_type' => "Individual",
'first_name' => "first",
'last_name' => "last",
'image_URL' => "<url>",
));
If you're on version < 5.0, the contact summary page will render the image only iff the URL is in form of http://site-name/civicrm/contact/imagefile?photo=imageName.jpg
and your image file should be present in your custom file upload directory.
There is now a patch(included in version 5.0) which allows the contact summary page to display any URL image irrespective of the photo
param in the URL.
If you want to download a copy of a remote image to your local file system and use that, you could do something along the lines of:
$contactId = 123;
$imageName = 'jitendra.jpg';
$path = Civi::paths()->getPath(Civi::settings()->get('customFileUploadDir')) . $imageName;
file_put_contents($path, file_get_contents('https://i.stack.imgur.com/yjnic.jpg'));
civicrm_api3('Contact', 'create', [
'id' => $contactId,
'image_URL' => CRM_Utils_System::url('civicrm/contact/imagefile', ['photo' => $imageName], true)
]);