# PHP Code Example

**URL:** <https://forum.alpaca.markets/t/php-code-example/1643>\
**Category:** Alpaca Integration Applications\
**Created:** [May 31, 2020, 6:10am UTC](https://forum.alpaca.markets/t/php-code-example/1643 "2020-05-31T06:10:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blazo](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Blazo](https://forum.alpaca.markets/u/Blazo)\
**Post date:** [May 31, 2020, 6:10am UTC](https://forum.alpaca.markets/t/php-code-example/1643/1 "2020-05-31T06:10:23Z")

</div>

Am I able to use Alpaca API to make a trade from a PHP page on a web server?

I only see tutorials and full code example for Python which I do not use. Does anyone have a full code example for Javascript using PHP and a web server?

For example I want to be able to go to a URL like this to issue a limit order: [https://www.myswebsite.com/createlimitorder.php?stock=AAPL&type=buy&quantity=10&password=mypassword](https://www.myswebsite.com/createlimitorder.php?stock=AAPL&type=buy&quantity=10&password=mypassword)

So when I hit that URL using my password for security, it would create a limit buy order for Apple for 10 shares.

I only need ONE full code example and I can customize and configure the rest but I have been searching for days now and asking the community in Slack and no one has responded.

-Chris

---

<div class="post-metadata">

**Author:** ![kris](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.alpaca.markets/kris/32/163_2.png) [@kris](https://forum.alpaca.markets/u/kris)\
**Post date:** [May 31, 2020, 5:15pm UTC](https://forum.alpaca.markets/t/php-code-example/1643/2 "2020-05-31T17:15:33Z")

</div>

you need php 7 and guzzle. i have it set up in the www/stock\_market/api if you look at [GMS Auto Trade V2 is here!](https://forum.alpaca.markets/t/gms-auto-trade-v2-is-here/874)  
theres a file called alpaca.php  
this is what i did to do limit orders, let me know if you need anything!

`\<?php  
//kris galante [krisgcell@gmail.com](mailto:krisgcell@gmail.com)  
//include(‘c:/php/WWW/Stock\_Market/config.php’);

```
set_time_limit ( 30 );

```

ini\_set(‘display\_errors’, ‘0’);

ini\_set(‘log\_errors’, ‘0’);

require ‘./vendor/autoload.php’;

require ‘./src/Alpaca.php’;  
require ‘./src/Response.php’;  
use Alpaca\Alpaca;

```
	$ACC_SETING = "C:/php/www/Stock_Market/API/ALPACA_TYPE.DATA"; // I was running the code on localhost, and hence the path!
$api_acc_type = file_get_contents($ACC_SETING);

```

if ($api\_acc\_type ==“Live”){  
$set\_alpaca\_acc\_type = False;

}  
if($api\_acc\_type ==“Paper”){  
$set\_alpaca\_acc\_type = True;

}

```
	$key_file = "C:/php/www/Stock_Market/API/alpaca.KEY"; // I was running the code on localhost, and hence the path!
	$key_secret = "C:/php/www/Stock_Market/API/alpaca.SECRET"; // I was running the code on localhost, and hence the path!
$api_key = file_get_contents($key_file);
$api_secret = file_get_contents($key_secret);

										//Paper trading

```

$alpaca = new Alpaca($api\_key, $api\_secret, $set\_alpaca\_acc\_type);

Trading\_name = \_POST[‘Trading\_name’];  
symbol = \_POST[‘symbol’];  
dirty\_price = \_POST[‘price’];  
ORDER\_TYPE = \_POST[‘ORDER\_TYPE’];  
quantity = \_POST[‘quantity’];

```
$get_action = $_GET['action'];

```

if ($dirty\_price \< 1) {  
$price = round($dirty\_price,4, PHP\_ROUND\_HALF\_UP);

}else {  
$price = round($dirty\_price,2, PHP\_ROUND\_HALF\_UP);  
}  
$resp = $alpaca-\>getAccount();  
if ($get\_action==‘trade’){

```
if ( $ORDER_TYPE== "buy") {

```

//if we are buying a stock

$test = json\_encode(($alpaca -\>createOrder($symbol,  
$quantity,  
$ORDER\_TYPE,  
‘limit’, //limit  
‘day’,  
$price, //limit\_price  
null,  
null)-\>getResponse()));

```
}

//if we are selling a stock, SEND IT AS A MARKET ORDER 

if ($ORDER_TYPE== "sell"){

```

$test = json\_encode(($alpaca -\>createOrder($symbol,  
$quantity,  
$ORDER\_TYPE,  
‘market’, //limit  
‘day’,  
null, //limit\_price  
null,  
null)-\>getResponse()));

```
}

```

}if (!isset($test)){  
$account = json\_encode(($alpaca -\>getAccount()-\>getResponse()));}  
//echo $get\_action;  
if ($test \<\>""){echo $test;}else{  
echo $account;}
