<?php
/*
* generate_php_developer_contract.php
*
* @(#) $Id: generate_php_developer_contract.php,v 1.1 2023/11/29 06:45:44 mlemos Exp $
*
*/
/*
*
* Use the vendor/autoload.php script instead of requiring the
* markdown parser and markup parser classes if you want to use
* Composer to install the packages necessary to run this script
*
*/
// require('vendor/autoload.php');
require_once('markdown_parser.php');
require_once('markup_parser.php');
/*
*
* Obtain an API key from Google Maker suite site:
* https://makersuite.google.com/app/apikey
*
*/
$api_key = '';
if($api_key === '')
{
die('Please go to Google Maker suite site to obtain an API key: https://makersuite.google.com/app/apikey');
}
/*
*
* The prompt is the request that you want Google generative API to
* perform. You may change this prompt text according to what you want
* to be generated.
*
*/
$prompt = "Write a PHP developer contract";
/*
*
* These are template variable values that will be use to replace
* in the text generated by the Google generative API.
*
*/
$client_name = 'Customer company name';
$client_address = 'Customer company address';
$developer_email_address = 'mlemos@acm.org';
$developer_name = 'Manuel Lemos';
$developer_address = 'Developer company address';
$amount = 'USD $1000';
$request = new stdClass;
$request->prompt = new stdClass;
$request->prompt->text = $prompt;
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-Type: application/json\r\n",
'content'=>json_encode($request)
)
);
$context = stream_context_create($opts);
$generated_php_developer_contract_template_json = file_get_contents('https://generativelanguage.googleapis.com/v1beta3/models/text-bison-001:generateText?key='.$api_key, false, $context);
if($generated_php_developer_contract_template_json === false)
{
$error = error_get_last();
die('Could not access the Google Generative language API: '.$error['message']);
}
$generated_php_developer_contract_template = json_decode($generated_php_developer_contract_template_json);
if(count($generated_php_developer_contract_template->candidates) === 0)
{
die('Google generative API returned no results for the request of: '.$prompt);
}
/*
*
* Google generative API may generate multiple candidates for the
* requested text. Here we are considering just the first candidate.
*
*/
$generated_php_developer_contract_template_markup = $generated_php_developer_contract_template->candidates[0]->output;
/*
*
* Replace template variable values to adjust the generated text
* according to your needs.
*
*/
$generated_php_developer_contract_markup = str_replace(array(
'[Client Name]',
'[Address]',
'[Email Address]',
'[PHP Developer Name]',
'[Address]',
'[amount]',
'[Client Address]',
'[Developer Name]',
'[Developer Address]',
'[Client Signature]',
'[Client Printed Name]',
'[Developer Signature]',
'[Developer Printed Name]',
), array(
$client_name,
$client_address,
$developer_email_address,
$developer_name,
$developer_address,
$amount,
$client_address,
$developer_name,
$developer_address,
$client_name,
$client_name,
$developer_name,
$developer_name
),
$generated_php_developer_contract_template_markup
);
/*
*
* The resulting text is in the Markdown format. If you want to use
* the text in a HTML page or another format, you need to convert the
* text to that format.
*
*/
$markdown=new markdown_parser_class;
$markdown->track_lines = 1;
$parameters=array(
'Data'=>$generated_php_developer_contract_markup
);
if(($success = $markdown->StartParsing($parameters)))
{
$markup = new markup_parser_class;
$html = '';
do
{
if(!($success = $markdown->Parse($end, $elements)))
break;
foreach($elements as $element)
{
if(!($success = $markup->RewriteElement($element, $element_html)))
break 2;
$html .= $element_html;
}
}
while(!$end);
if($success)
$success = $markdown->FinishParsing();
}
echo '<html><head><title>Generated PHP developer contract</title></head><body>';
if($success)
{
echo $html;
}
else
{
echo '<h1>Markdown parsing error: '.HtmlSpecialChars($markdown->error).' at position '.$markdown->error_position;
if($markdown->track_lines
&& $markdown->GetPositionLine($markdown->error_position, $line, $column))
echo ' line '.$line.' column '.$column;
echo "</h1>\n";
}
for($warning = 0, Reset($markdown->warnings); $warning < count($markdown->warnings); Next($markdown->warnings), $warning++)
{
$w = Key($markdown->warnings);
echo '<p>Warning: ', HtmlSpecialChars($markdown->warnings[$w]), ' at position ', $w;
if($markdown->track_lines
&& $markdown->GetPositionLine($w, $line, $column))
echo ' line '.$line.' column '.$column;
echo "</p>\n";
}
echo '</body></html>';
?>
|