Adding subscriber to dotmailer address book - php

I'm having issues adding a subscriber to a specific addressbook in dotmailer using nusoap. I've no problem adding a generic subscriber to all contacts using the CreateContact method however when I try and use the AddContactToAddressbook method, I just doesn't work.
The if statement used at the bottom returns successful, however nothing exists in the $result variable.
<?php
function subscribe($email, &$result)
{
global $postURL, $username, $password;
$addressBookId = "######";
$contact = array("Email" => $email, "EmailType" => "Html");
$params = array("username" => $username, "password" => $password, "contact" => $contact, "addressbookId" => $addressBookId);
$client = new soapclient($postURL, true);
$error = $client->getError();
$result = $client->call('AddContactToAddressbook', $params);
if($client->fault) {
$rv = false;
} else {
// Check for errors
if($error) {
$rv = false;
} else {
$rv = true;
}
}
return $rv;
}
if(subscribe("test#test.com", $result)) {
echo "success<br />";
print_r($result);
} else {
echo "failed<br />";
}
?>
This code works as is with only changing
$result = $client->call('AddContactToAddressbook', $params);
to
$result = $client->call('CreateContact', $params);
But then the subscriber is not in any particular list. Does anyone know what I might be doing wrong.
p.s. the $addressBookId variable has been blanked out intentionally, I didn't try to run it with '######' as the value in case you were wondering.
And once in production the $result variable will not be returned with the function.
Thanks

use
$result = $client->call('AddContactToAddressBook', $params);
capital B on book

Related

Getting synxtax error when trying to echo sql results as json datatype

I'm trying to output the sql results of the statement in json datatype, but an error was prompt SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data, I have checked my sql statement and try to executed in sql editor and it works, but im not sure why it doesnt work when im trying to echo it in the browser.
So i have a big switch statement block, and this is a small part of it, so basically when i type http://localhost/w11/local-html/part_1/api/schedule it should output json datatype by taking in the arguments from the url like api/schedule and api is arg_1 and schedule is arg_2.
case 'api':
{
header("Content-Type: application/json");
switch ($param2) {
case 'schedule':
{
switch ($param3) {
case '':
{
try {
$sqlQuery = "SELECT room, type, title, day, time FROM sessions INNER JOIN slots ON sessions.slotsID = slots.id";
$response = new JSONRecordSet();
$response = $response->getJSONRecordSet($sqlQuery, "");
echo $response;
} catch (PDOException $e) {
echo "Connection Failed:" . $e->getMessage();
}
break;
}
default:
{
//do something
break;
}
}
break;
}
This is the function for getJSONRecordSet
class JSONRecordSet extends RecordSet {
function getJSONRecordSet($sql, $params = null) {
$queryResult = $this->getRecordSet($sql, $params);
$recordSet = $queryResult->fetchAll(PDO::FETCH_ASSOC);
$nRecords = count($recordSet);
if ($nRecords == 0) {
$status = 200;
$message = array("text" => "No records found");
$result = '[]';
}
else {
$status = 200;
$message = array("text" => "");
$result = $recordSet;
}
return json_encode(
array(
'status' => $status,
'message' => $message,
'data' => array(
"RowCount"=>$nRecords,
"Result"=>$result
)
),
JSON_PRETTY_PRINT
);
}
}
And the parent class
abstract class RecordSet {
protected $conn;
protected $queryResult;
function __construct() {
$this->conn = pdoDB::getConnection();
}
function getRecordSet($sql, $params = null) {
if (is_array($params)) {
$this->queryResult = $this->conn->prepare($sql);
$this->queryResult->execute($params);
}
else {
$this->queryResult = $this->conn->query($sql);
}
return $this->queryResult;
}
}
So, how do i tackle this error?
There is nothing wrong with this code.
Since SyntaxError: JSON.parse is a JS error you should just check what data is passed to it. Check the raw response to your request and do some debugging.
This is probably a problem with your db connection or your $param2 or $param3 conditions are not met.

PHP, not display all inside my array, but only first resulting

$projects = array('1' => $link1, '2' => $link2);
function server_status($projects) {
foreach ($projects as $server) {
$api_status = ping_api($server);
if ($api_status == 1) {
$json = json_decode(file_get_contents($server), true);
foreach ($json as $obj) {
if ($obj['online'] < 1) {
// OFFLINE
return -1;
}
else {
// ONLINE
return $obj['online'];
}
}
} else {
// MAINTENANCE
return 0;
}
}
}
function cache_status($data) {
echo $data;
$servername = "localhost";
$username = "root";
$password = "";
try {
$conn = new PDO("mysql:host=$servername;dbname=test", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
foreach ($data as $status) {
// server id + status
$sql = "UPDATE projects SET status=:status WHERE id=:server_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':status', $status, PDO::PARAM_INT);
$stmt->bindParam(':server_id', 1, PDO::PARAM_INT);
$stmt->execute();
}
}
$problem = array(server_status($projects));
print_r($problem);
My problem is, when I print_r the variable $problem it should return two results in array, since there are two results but it only returning the first result from array as an .. example: Array ( [0] => 260 )
IF anyone is kind enough to look at my code and tell me what I am doing wrong, I would be very greatful
You are using return statement inside your loop that is why it breaks your loop on first iteration and returns what ever you have mentioned to the original call of function. To resolve this issue you need to collect the response from each iteration in array and in the end of function return your response for each iteration something like
function server_status($projects) {
$response= array();
$status = 0; // default status
foreach ($projects as $server) {
$api_status = ping_api($server);
if ($api_status == 1) {
$json = json_decode(file_get_contents($server), true);
foreach ($json as $obj) {
if ($obj['online'] < 1) {
// OFFLINE
$status= -1;
}
else {
// ONLINE
$status = $obj['online'];
}
}
}
$response[] = array('server'=>$server,'status'=>$status);
}
return $response;
}

Retrive a value of a session stored in an associative array CodeIgniter

I am trying to retrieve the primary key that I stored in a session but I can't find a way to print it.
Here is my controller code
$usr_result = $this->Judge_Model->get_user($username, $password);
if ($usr_result > 0)
{
//setting the session variables
$sessiondata = array('login' => TRUE,'JudgeName' => $username,'uid' => $uresult[0]->JudgeID);
$sessiondata = array($this->session->set_userdata($sessiondata));
redirect("JudgingApp/MainMenu");
My model
function get_user($usr, $pwd)
{
$sql = "select * from Judge where JudgeName = '" . $usr . "' and JudgePass = '" . $pwd . "' ''";
$query = $this->db->query($sql);
return $query->num_rows();
}
and the page that I want to print it on
public function MainMenu()
{
$data['Poster'] = $this->Judge_Model->get_Posters();
$this->load->view('JudgingApp/MainMenu',$data);
if (isset($this->session->userdata['JudgeName']))
{
echo $this->session->userdata['JudgeName'];
echo $this->session->userdata['uid'];
echo $this->session->userdata['Pass'];
}
else
{
echo nothing;
}
}
I just want to print for debugging purposes and ensure that the value is being passed not so much that I want to show the user their ID. I need the PK for another function so I need to make sure I can access it.
Try this
In Controller
$usr_result = $this->Judge_Model->get_user($username, $password);
if (!empty($usr_result))
{
$sessiondata = array(
'login' => TRUE,
'JudgeName' => $username,
'uid' => $uresult[0]['JudgeID']
);
if (!$this->session->set_userdata($sessiondata)) {
echo "Failed to set session";
} else {
redirect("JudgingApp/MainMenu");
}
}
else
{
echo "No Data Found";
}
public function MainMenu()
{
$data['Poster'] = $this->Judge_Model->get_Posters();
if (isset($this->session->userdata['JudgeName']))
{
# To print this on view you need to add this in view. Not in controller. Or Bind it to $data and pass it
echo $this->session->userdata['JudgeName'];
echo $this->session->userdata['uid'];
echo $this->session->userdata['Pass'];
}
else
{
echo nothing;
}
$this->load->view('JudgingApp/MainMenu',$data);
}
In Model
function get_user($username, $password)
{
$array = array('JudgeName' => $username, 'JudgePass' => $password);
$this->db->where($array);
$query = $this->db->get('Judge');
$result = $query->result_array();
return $result;
}

Getting no database selected message. PHP,mysql

I have a database at local server. I am trying to access this database. I have table of venueTypeMaster in the database. I tried to get all the venuetypes from database. But when I run the script message I get is - 'No database selected'.
I have a class in which I have created two functions in php script. For createVenue it dose not send any message and the query has been executed successfully. But for getVeunueTypes it is showing this message. Connection for both is same.
DB Script:
<?php
include_once("config.php");
include_once("exceptions.php");
class DB {
static $con;
public static function getConnection() {
try {
if ( DB::$con == null ) {
DB::$con = mysqli_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD);
if ( DB::$con) {
$status = mysqli_select_db(DB::$con,DB_NAME);
if (! $status ) throw new DBSelectionExcpetion(mysqli_error());
return DB::$con;
} else {
$e = new DBConnectionException(mysqli_error());
throw $e;
}
}
return DB::$con;
} catch(DBConnectionException $e) {
throw $e;
} catch(DBSelectionExcpetion $de) {
throw $de;
}
return null;
}
?>
venueTypes script
<?php
include("exceptions.php");
include("DB.php");
include("config.php");
class VenueTypes {
public function getVenueTypes() {
try {
$con = DB::getConnection();
$query = "select * from venueTypeMaster";
$rs = mysql_query($query) or die (json_encode(array("result"=>-1, "message"=>mysql_error())));
$n = mysql_num_rows($rs);
$vt = array();
if ( $n > 0 ) {
while ( $row = mysql_fetch_assoc($rs)) {
$vt[] = $row;
}
$result = array("result"=>1, "message"=>"success", "venueTypes" => $vt);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Venue types list is empty");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
public function createVenueType($fields) {
try {
$required = array("venuetype", "active", "entry_by", "entry_date", "entry_time", "last_modify_date", "ip_addr","venue_name");
$actual = array_keys($fields);
$intersection = array_intersect($required, $actual);
$n1 = sizeof($required);
$n2 = sizeof($intersection);
if ( $n1 != $n2 ) {
throw new MissingParameterException();
}
$con = DB::getConnection();
$keys = array_keys($fields);
$values = array_values($fields);
$columns = implode(",", $keys);
$n = sizeof($values);
for($i=0;$i<$n; $i++) {
$values[$i] = "'" . mysqli_real_escape_string($con,$values[$i]) . "'";
}
$columnValues = implode(",", $values);
$query = "insert into venuetypemaster($columns) values($columnValues)";
mysqli_query($con,$query) or die (json_encode(array("result"=>-1, "message"=>mysqli_error())));
$af = mysqli_affected_rows($con);
if ( $af == 1 ) {
$venuetypeId = mysqli_insert_id($con);
$result = array("result"=>1, "message"=>"success", "venuetypeId" => $venuetypeId);
return json_encode($result);
} else {
$result = array("result"=>-1, "message"=>"Failed to register");
return json_encode($result);
}
} catch(DBConnectionException $e) {
$result = array("result"=>-1, "message"=> $e -> getMessage());
return json_encode($result);
}
return null;
}
?>
getVenueType script
<?php
header("Content-type: application/json");
if ( $_SERVER['REQUEST_METHOD']=='GET') {
include_once ("../include/VenueTypes.php");
try {
$v = new VenueTypes();
$response = $v -> getVenueTypes();
//$response is null means something went wrong as a result we got null result from above statement
if ( $response == null ) {
$response = json_encode(array("result" => -2, "message" => "Empty result"));
echo $response;
} else {
echo $response;
}
} catch(Exception $e) {
$result = array("result" => -1, "message" => $e -> getMessage());
echo json_encode($result);
}
} else {
$result = array("result" => -3, "message" => "Unsupported method : " . $_SERVER['REQUEST_METHOD']);
echo json_encode($result);
}
?>
Can anyone help please.. Thank you..
The problem is that you are mixing between the mysqli_xxx() and mysql_xxx() functions. These two libraries are not compatible with each other; you must only use one or the other throughout your code.
Since the mysql_xxx() functions are obsolete and deprecated, that means you should only use mysqli_xxx().
Change all occurrences of mysql_ in your code to mysqli_.
You will also need to make other changes to these lines of code -- notably this will include adding the DB reference variable (ie the variable returned by getConnection()), but some other syntax changes may also be necessary.

How can I use a for loop in PHP to return mysql data

I am making an app that will list employees by a certain store in a listview. My current function in my DB_Functions.php file is:
public function getEmployeeList($name) {
$stmt = $this->con->prepare("SELECT employee_name FROM employees WHERE name = ?");
$stmt->bind_param('s', $name);
if ($stmt->execute()) {
$employee_list = $stmt->get_result()->fetch_assoc();
$stmt->close();
if (empty($employee_list)) {
return NULL;
} else {
return $employee_list;
}
}
}
and in my employees.php file I have the following code:
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array('error' => FALSE);
if (isset($_POST['name'])) {
$name = $_POST['name'];
$employee_list = $db->getEmployeeList($name);
if ($employee_list != false) {
$response['error'] = FALSE;
//EMPLOYEE LIST OBJECT HERE
} else {
$response['error'] = TRUE;
$response['error_msg'] = 'No employees have been added to this profile.';
echo json_encode($response);
}
} else {
$response['error'] = TRUE;
$response['error_msg'] = 'You have not logged in to your store\'s account, please log in first.';
echo json_encode($response);
}
?>
I would like to have an employee_list object in the commented space above. Something like:
$response['employee_list']['0'] = $employee_list['0'];
$response['employee_list']['1'] = $employee_list['1'];
$response['employee_list']['2'] = $employee_list['2'];
etc... etc...
After that JSONObject is returned to the android app, the contents will be listed in a listview. I would need a for loop (I think) because the employee number will never be known since each store will be able to add and remove employees as they wish. Can someone point me in the right direction and also advise if I am using the correct approach as far as the rest of the code. Thanks.
First, in your DB_Functions.php, you should be returning the mysqli_result object.
Hence your DB_Functions should be this:
public function getEmployeeList($name) {
$stmt = $this->con->prepare("SELECT employee_name FROM employees WHERE name = ?");
$stmt->bind_param('s', $name);
if ($stmt->execute()) {
// we get the mysqli_result object without calling the fetch_assoc() on it
$result = $stmt->get_result();
$stmt->close();
// if the count is less than 1, no result found, hence return null
if ($result->num_rows < 1) {
return null;
} else {
// we return the mysqli_result object without calling the fetch_assoc() on it
return $result;
}
}
}
In your employees.php, what you want is something like this:
<?php
require_once 'include/DB_Functions.php';
$db = new DB_Functions();
$response = array('error' => FALSE);
if (isset($_POST['name'])) {
$name = $_POST['name'];
$result = $db->getEmployeeList($name);
// do an early check for if result returns null or is not set
if (is_null($result) || !$result) {
$response['error'] = TRUE;
$response['error_msg'] = 'No employees have been added to this profile.';
} else {
$response['error'] = FALSE;
//EMPLOYEE LIST OBJECT HERE
// since $result->fetch_assoc() returns one row at a time, you want to loop through each row and get the appropriate data
while ($row = $result->fetch_assoc()) {
// inject the current into the employee_list array
$response['employee_list'][] = $row;
}
}
} else {
$response['error'] = TRUE;
$response['error_msg'] = 'You have not logged in to your store\'s account, please log in first.';
}
// echo response gets called no matter what
echo json_encode($response);
Hope it helps

Categories