Reputation: 3907
I'm trying to create Lead into Odoo v8, from a php
form.
From this form I send the message via POST
to an xml-rpc client, then this client sends it to Odoo and creates a Lead on it.
I'm trying hard but with no success so far, this is my form.php
code:
<html>
<head>
<title>Contact form</title>
</head>
<body>
<h4>Contact Form</h4>
<form method="post" action="crmlead.php">
<label for="firstname">Firstname</label>
<input type="text" name="name" value="" id="name" class="required text" title="Please, fill in your firstname" data-required="true"><br />
<label for="partner_name">Partner Name</label>
<input type="text" name="partner_name" value="" id="partner_name" class="required text" title="Please, fill in your company name" data-required="true"><br />
<label for="event_date">Event Date</label>
<input type="text" name="event_date" value="" id="event_date" class="required text" title="Please, fill in your job title" data-required="true"><br />
<label for="email">Email</label>
<input type="text" name="email" value="" id="email" class="required email text" title="Please, enter a valid email address" data-required="true"><br />
<label for="phone">Phone</label>
<input type="text" name="phone" value="" id="phone" class="required phone text" title="Please use international format (eg: +32...)" data-required="true">
<label for="guests">Guests</label>
<input type="text" name="guests" value="" id="guests" class="required text" title="Please, fill in your city" data-required="true"><br />
<label for="budget">Budget</label>
<td><input type="text" name="budget" value="" id="budget" class="required text" title="Please, fill in your zipcode" data-required="true"><br />
<label for="description">Description</label>
<td><input type="text" name="description" value="" id="description" class="required text" title="Please, fill in your zipcode" data-required="true"><br />
<label for="location">Location</label><input type="text" name="location" value="" id="location" class="text" title="Please, fill in your state"> <br />
<p>
<input type="submit" value="Send"/> <input type="reset" />
</p>
</form>
</body>
</html>
And this is my crmlead.php
client code:
<?php
include("xmlrpc.inc");
class Contact
{
private $subject = '', $to = '';
function __construct($to, $sub)
{
$this->to = $to;
$this->subject = $sub;
}
function xmlCallTo($usr, $password, $database, $server, $post)
{
$user = "admin";
$pass = "admin";
$db = "BMWE-001";
$server_url = 'http://localhost:8069/xmlrpc/';
$client = new xmlrpc_client($server_url.'common');
$msg = new xmlrpcmsg('login');
$msg->addParam(new xmlrpcval($db, "string"));
$msg->addParam(new xmlrpcval($user, "string"));
$msg->addParam(new xmlrpcval($pass, "string"));
$res = &$client->send($msg);
if(!$res->faultCode()){
$val = $res->value();
$id = $val->scalarval();
if (empty($id)){
echo "Connection error = ";
exit;
}
else
{
$val = $res->value()->scalarval();
$val = array ("name" => new xmlrpcval($post["New Lead","string"),
"email_from" => new xmlrpcval($post['email'], "string"),
"phone" => new xmlrpcval($post['phone'], "string"),
"partner_name" => new xmlrpcval($post['partner_name'], "string"),
"event_date" => new xmlrpcval($post['event_date'], "string"),
"guests" => new xmlrpcval($post['guests'], "string"),
"location" => new xmlrpcval($post['location'], "string"),
"budget" => new xmlrpcval($post['budget'], "string"),
"stage_id" => new xmlrpcval(2, "int"),
"state" => new xmlrpcval("draft", "string"),
"description" => new xmlrpcval($post['description'], "string")
);
$msg = new xmlrpcmsg('execute');
$msg->addParam(new xmlrpcval($db, "BMWE-001"));
$msg->addParam(new xmlrpcval($id, "1"));
$msg->addParam(new xmlrpcval($pass, "admin"));
$msg->addParam(new xmlrpcval("crm.lead", "string"));
$msg->addParam(new xmlrpcval("create", "string"));
$msg->addParam(new xmlrpcval($val, "struct"));
$res2 = &$client2->send($msg);
if(!$res2->faultCode())
{
$readVal = $res2->value()->scalarval();
}
else
{
echo "<br />Lead is not created";
}
}
else
{
echo "<br />Problem in message sending for create lead";
}
}
}
else
{
echo "<br />Connection not established";
}
}
}
exit;
?>
Both of these Scripts run on same server, on localhost, thru nginx
the php configuration runs just fine, so, nginx server is ok on that part.
And Odoo v8 is running on same machine, independently, localhost also, obviously.
Upvotes: 1
Views: 2334
Reputation: 3378
The issue is likely that you are not calling the create method on the model which creates leads. The code above looks like it will simply log into your Odoo server. This would likely return the uid of the user you logged in as.
You need to log in and then pass an array of variables (for your lead), the model in question, and then the function you wish to execute on this model using the arguments you are passing. Because you are wishing to create a new lead this would be the create function. If you were editing a lead it would be the write function. The link below contains examples on how to accomplish what you are hoping.
This should accomplish what you are looking for(I borrowed this from the link above). You will want to change the reference to res.partner to the model you need. You will also change the array of variables to the values from your form.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
echo '<h2>XML-RPC AVEC OPENERP/ODOO ET PHP</h2>';
include("xmlrpc_lib/xmlrpc.inc.php");
include("xmlrpc_lib/xmlrpcs.inc.php");
$GLOBALS['xmlrpc_internalencoding']='UTF-8';
$user = 'admin';
$password = 'mY5up3rPwd';
$dbname = 'test';
$server_url = 'http://192.168.1.120:8069';
$connexion = new xmlrpc_client($server_url . "/xmlrpc/common");
$connexion->setSSLVerifyPeer(0);
$c_msg = new xmlrpcmsg('login');
$c_msg->addParam(new xmlrpcval($dbname, "string"));
$c_msg->addParam(new xmlrpcval($user, "string"));
$c_msg->addParam(new xmlrpcval($password, "string"));
$c_response = $connexion->send($c_msg);
if ($c_response->errno != 0){
echo '<p>error : ' . $c_response->faultString() . '</p>';
}
else{
$uid = $c_response->value()->scalarval();
$val = array (
"name" => new xmlrpcval("Godin Thierry", "string"),
"street" => new xmlrpcval("Au fond à gauche", "string"),
"city" => new xmlrpcval("Marne la Vallée", "string"),
"zip" => new xmlrpcval("77000", "string"),
"website" => new xmlrpcval("http://www.lapinmoutardepommedauphine.com", "string"),
"lang" => new xmlrpcval("fr_FR", "string"),
"tz" => new xmlrpcval("Europe/Paris", "string"),
);
$client = new xmlrpc_client($server_url . "/xmlrpc/object");
$client->setSSLVerifyPeer(0);
$msg = new xmlrpcmsg('execute');
$msg->addParam(new xmlrpcval($dbname, "string"));
$msg->addParam(new xmlrpcval($uid, "int"));
$msg->addParam(new xmlrpcval($password, "string"));
$msg->addParam(new xmlrpcval("res.partner", "string"));
$msg->addParam(new xmlrpcval("create", "string"));
$msg->addParam(new xmlrpcval($val, "struct"));
$response = $client->send($msg);
echo 'Partner created - partner_id = ' . $response->value()->scalarval();
}
?>
Upvotes: 1