HTTP functions

The HTTP functions can retrieve content - prices, signals, news, or indicators such as VIX or COTR - from external websites and use them in the trading strategy. They can also execute PHP scripts f.i. for sending emails from a web server (Zorro 1.16 and above).

http_transfer (string url, string data): string

Transfers data to or from the web address given by url. The function waits until the web page content or the PHP script response is transferred back to Zorro. The data string can contain arguments to be passed to a PHP script (such as the content of an email). The function returns a temporary string containing the web page content for further evaluation.

http_send (string url, string data, string header): int

Starts a HTTP_GET (when data == 0) or HTTP POST (when data != 0) data transfer to or from a web address. The function returns a identifier number (id) for controlling the data transfer with http_status. The data string can contain arguments to be passed to the PHP script (such as the content of an email). If the data string begins with a '#' character, the following word is used for the request type instead of POST (f.i. "#DELETE ..." or "#PATCH ..."). The header string contains a custom header f.i. for authorization, or 0 for not using a custom header. After the transfer is finished, the identifier number must be freed with http_free.

http_post (string url, string data): int

Starts a HTTP POST data transfer to or from a web address. The function returns a identifier number (id) for controlling the data transfer with http_status. The data string can contain arguments to be passed to the PHP script (such as the content of an email). After the transfer is finished, the identifier number must be freed with http_free.

http_proxy (string proxy, int port)

Sets up a HTTP proxy server for the connection to the Internet. If no proxy is used, or if you don't know what a HTTP proxy is, you won't need this function.

http_status (int id): long

Returns the number of bytes received with a HTTP data transfer with the identifier number id, or 0 if the transfer is still in progress, or -1 if the transfer failed, or -2 if the identifier number is invalid.

http_result (int id, string content, long size): long

Stores the received data of a HTTP data transfer in the content string, up to size bytes, and returns the received number of bytes.

http_free (int id)

Frees the identifier number of a HTTP data transfer and stops the transfer. Must be called at the end of each HTTP data transfer. For stopping the last transfer, set id to 0.

Parameters:

proxy - proxy server name (example: "proxy-host.com").
port - port of the proxy server (example: 8080).
url - URL of the web page (example: "http://opserver.de/scratch/ip.php")
data - string (or char array) containing the data to be sent (example: "user=John&password=Secret"), or 0 for no data.
content - string (or char array) to receive the web page content or the result data of a HTTP POST operation.
size - maximum number of characters to be received, or 0 for using the actual content string length.
id - identifier number, returned by http_post().

Remarks:

Examples:

// access the CFTC website, read the current Commitment Of Traders Report,
// and store it in a file.
function main()
{ file_delete("cot.txt"); // delete previous report file_append("cot.txt", http_transfer("http://www.cftc.gov/dea/futures/deacmesf.htm",0)); }
// Download historical price data from Yahoo
function main()
{
  int id = http_post("http://ichart.finance.yahoo.com/table.csv?s=AAPL&c=1980",0);
  if(!id) return;
  while(!http_status(id))
    if(!wait(100)) return; // wait for the server to reply
  int length = http_status(id);
  if(length > 0) { //transfer successful?
    string content = (string)malloc(length);
    http_result(id,content,length); // store price data in large string
    file_write("History\\AAPL.csv",content,length);
    free(content); // release the string
  }
  http_free(id); //always clean up the id!
}
// start the script "ip.php" on a remote server, and 
// print dots until Zorro's IP address is returned
function main() { char ip_str[100]; // just a long empty string int id = http_post("http://opserver.de/scratch/ip.php",0); if(!id) return; while(!http_status(id)) { if(!wait(100)) return; // wait for the server to reply printf("."); }
if(http_status(id) > 0) { //transfer successful? http_result(id,ip_str,100); //get the replied IP printf("\n%s",ip_str); } else printf("\nError during transfer!"); http_free(id); //always clean up the id! } ip.php: <?php
echo "Your IP address: " . $_SERVER['REMOTE_ADDR'];
?>
// send an email when a trade is entered
function sendEmailAboutTrade()
{
// compose the message
  char content[1000];
  sprintf(content,"content=Zorro has entered a trade!\n%d Lots of %s",
   TradeLots,TradeAsset);
  http_transfer("http://www.myserver.com/zorromail.php",content);
}

zorromail.php:
<?php
$to = "me@myself.com";
$subject = "Message from Zorro!";
$body = $_POST['content'];
mail($to,$subject,$body);
?>

See also:

ftp functions, file functions

► latest version online