How to send portion of funds from a private key programmatically? I'm building an app and one of my features is being able to scan a bitcoin private key and then send a portion of its funds to another address. How can I do something like this in javascript or php? Do I need to run my own Bitcoin server? 

Is there an example script I can use where I can just plug in a private key?
You can either use your own bitcoind server or you can use a 3rd party API service.

Let's do a few examples of what you are asking.
I'll use short fake addresses and private keys as examples to make things easy.    

PHP with a bitcoind server

&lt;?php
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//add the private key (label, key)
$import = bitcoin-&gt;importprivkey("5KL3cN..SOME_PRIVATE_KEY", "Satoshi1");
?&gt;    


What we did here was import a private key and give it a specific unique label.  This part might take a while as your node will need to rescan as you added a new private key and it needs to check for transactions. 

Once the node is rescanned you can send the funds.

&lt;?
require("easybitcoin.php");
$bitcoin = new Bitcoin("username", "somepassword");

//send the funds from the Satoshi1 account/label
$send = $bitcoin-&gt;$bitcoin-&gt;sendfrom("Satoshi1","1receivingAddress", 0.5);
echo $send ? $send : "Oops an error: ".$bitcoin-&gt;error;
?&gt;    


What we did above was use the sendfrom() JSON RPC command to send 0.5BTC from the account/label Satoshi1 to the 1receivingAddress. If successful it will respond with the transaction ID, or if an error it will print out the error. 

This is just the code, you'll still need to set your VPS with bitcoind or you can run a node on your home computer and experiment locally using something like xampp. 

PHP with a 3rd party API

Here's an example with blockchain.info wallet API. You can import the private key using their create wallet utility, and then later send from that specific address.

$private = "5KL3cN..SOME_PRIVATE_KEY";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/create?password=YOUR-PASSWORD&amp;api_code=YOUR-API-CODE&amp;priv=".$private;
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //change to true if your host can verify SSL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "&lt;pre&gt;";
var_dump($json);
echo "&lt;/pre&gt;";


Then once created you can send from that specific new address that was imported. 

$from = "1fromAddress"; 
$to = "1receivingAddress";
$url = "http://127.0.0.1:3030/merchant/YOUR-GUID/payment?password=YOUR-PASSWORD&amp;api_code=YOUR-API-CODE&amp;from=".$from."&amp;amount=1000000&amp;to=".$to;
//amounts are in satoshi so 1000000 is 0.01BTC
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        
curl_setopt($ch, CURLOPT_URL, $url);
$ccc = curl_exec($ch);
$json = json_decode($ccc, true);
echo "&lt;pre&gt;";
var_dump($json);
echo "&lt;/pre&gt;";


If you need help setting up bitcoind or blockchain.info's wallet API I suggest you check out my tutorials on youtube. The hardest part with the 3rd party route is getting an API key; I hear blockchain.info has been strict with these lately.   

bitcoind server method: https://www.youtube.com/watch?v=cMM-t7azzJE
blockchain.info 3rd party method: https://www.youtube.com/watch?v=X8jsaf4sEgs