Consecutive queries in same connection not working - php

The following code is working just fine, but the line $connection->query('call user_create(145552, \'a#a.com\');'); is not creating a user into the database.
function updateFacebook($id) {
$connection = mysqli_connect('localhost', 'root', '', 'databasename');
$count = $connection->query('call user_by_facebook_read(' . $id . ');')->num_rows;
if ($count == 0) {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
$json = json_decode($body);
$token = $json->token;
$facebook = new Facebook(array(
'appId' => 'value',
'secret' => 'value',
'cookie' => true
));
$facebook->setAccessToken($token);
$facebook->api('/me/feed', 'post', array(
'message' => 'message',
'picture' => 'https://www.google.com.br/images/srpr/logo4w.png',
'link' => 'https://www.google.com.br',
'description' => 'description',
'name' => 'name',
'caption'=> 'caption'
));
$connection->query('call user_create(145552, \'a#a.com\');');
}
$connection->close;
$json = json_encode(array(
'r' => true
));
echo $json;
}
If I run the following code it also works just fine:
$connection = mysqli_connect('localhost', 'root', '', 'databasename');
$connection->query('call user_create(145552, \'a#a.com\');');
$connection->close;
If I close the connection after the $count and open it again before the user_create call it also works.
What is happening?

You need to run ->close() on the result, but you discarded that. So:
$result = $connection->query(..);
$count = $result->num_rows;
$result->close();
and after CALL()s in MySQLi it seems to need this:
$connection->next_result();

Related

Get error in Nusoap: XML error parsing SOAP payload

I'm new on Soap\NuSoap
I can connect to server site but from server send data back to client i can not get data in array to show client site. I have to test to get data and send data back to other database. I have to get in array.
it's show error
XML error parsing SOAP payload on line 2: Invalid document end
I can't fix it by myself. please help.
This is my client.php
include("lib/nusoap.php");
$client = new nusoap_client("http://192.168.20.3/soap_server/webservice.php?wsdl");
$client->soap_defencoding = 'utf-8';
$client->encode_utf8 = false;
$client->decode_utf8 = false;
$params = array(
'strName' => $_POST["strName"],
);
$result = $client->call("resultCustomer",$params);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
print_r($result);
And this is server.php
require_once("lib/nusoap.php");
//Define our namespace
$namespace = "http://localhost/soap_server/webservice.php";
//Create a new soap server
$server = new soap_server();
//Configure our WSDL
$server->configureWSDL("getCustomer");
$server->wsdl->schemaTargetNamespace = $namespace;
$server->soap_defencoding = 'utf-8';
$server->encode_utf8 = false;
$server->decode_utf8 = false;
//Register our method and argument parameters
$varname = array(
'strName' => "xsd:string"
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'',
array(
'id_user' => array('name' => 'id_user', 'type' => 'xsd:string'),
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'username' => array('name' => 'username', 'type' => 'xsd:string'),
'lastname' => array('name' => 'lastname', 'type' => 'xsd:string')
)
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:DataList[]')
),
'tns:DataList'
);
// Register service and method
$server->register('resultCustomer',$varname, array('return' => 'tns:ArrayOfString'));
function resultCustomer($strName)
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("qpt-test");
$strSQL = "SELECT * FROM qpt_user where firstname like '%".$strName."%'";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
return $resultArray;
}
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();

mysqli functions and external classes

Im trying to write a function that uses an external db class but its not going so well. The class works fine when its not in a function. Heres my code:
require_once('MysqliDb.php');
function add_post($userid,$body,$cat_id,$user_link){
$db = new MysqliDb('localhost', 'root', 'root', 'my_database');
$date = new DateTime();
$now = $date->getTimestamp();
$insertData = array(
'user_id' => $userid,
'body' => $body,
'stamp' => $now,
'cat_id' => $cat_id,
'link' => $user_link
);
if ( $Db->insert('posts', $insertData) ) echo 'success!';
}
Heres the call:
$userid = 1;
$body = "hey whats up this is from a db class";
$cat_id = 3;
$user_link = "http://www.aol.com";
add_post($userid,$body,$cat_id,$user_link);

XML error parsing SOAP payload on line 1: Not well-formed (invalid token)

I'm trying to create a webservice with PHP and nuSoap but everytime I try to execute it I'm getting the error:
XML error parsing SOAP payload on line 1: Not well-formed (invalid token)
Can anyone see what's wrong?
service.php
<?php
require 'lib/nusoap.php';
$server = new nusoap_server();
$server->configureWSDL("casamitger" . "urn:casamitger");
$server->wsdl->schemaTargetNamespace = 'urn:casamitger';
include 'functions.php';
//getAvailabilities
$server->wsdl->addComplexType('Availabilities','complexType','struct','all','',array(
'StartDate' => array('name' => 'StartDate', 'type' => 'xsd:date'),
'EndDate' => array('name' => 'EndDate', 'type' => 'xsd:date'),
'State' => array('name' => 'State', 'type' => 'xsd:string'),
));
$server->wsdl->addComplexType('ArrayOfAvailabilities', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Availabilities[]')), 'tns:Availabilities');
$server->register(
'getAvailabilities',
array(
"SessionID" => 'xsd:string',
"AccommodationId" => 'xsd:integer'
),
array("return" => 'tns:ArrayOfAvailabilities')
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
functions.php
function getAvailabilities($sessionID, $accommodation_code) {
$connection = mysqli_connect("localhost", "root", "", "casamitger");
if (authenticate($sessionID)) {
$user = getUser($sessionID);
$query = mysqli_query($connection, "SELECT count(AccommodationId) c FROM UserAccommodations WHERE AccommodationId = '$accommodation_code' AND CompanyId = '$user'") or die();
$row = mysqli_fetch_object($query);
$count = $row->c;
if ($count > 0) {
$query = mysqli_query($connection, "SELECT StartDate,EndDate,State FROM Availabilities WHERE AccommodationId = '$accommodation_code'") or die();
$n = 0;
while ($row = mysqli_fetch_object($query)) {
$result[$n]['StartDate'] = $row->StartDate;
$result[$n]['EndDate'] = $row->EndDate;
$result[$n]['State'] = $row->State;
$n++;
}
return $result;
}
}
}
and the client.php
<?php
require 'lib/nusoap.php';
include 'functions.php';
$sessionid = '1234';
$accommodation_code = '83081';
$client = new nusoap_client("http://192.168.8.155:8090/ws/service.php?wsdl");
$availabilities = $client->call(
'getAvailabilities',
array(
"SessionID" => "$sessionid",
"AccommodationId" => "$accommodation_code",
)
);
if ($client->fault) {
echo 'Fault';
} else {
$err = $client->getError();
if ($err) {
echo $err;
} else {
print_r($servicetypes);
}
}
?>
If I call the method getAvailabilities() directly it works but it doesn't through the web service, any help will be appreciated, thanks.

Setting setAuth() on Guzzle when sending parallel requests

When making requests in Guzzle you can use this to set username and password:
$request->setAuth($username, $password);
This worked great for my single requests. Now I need to make a set of parallel requests and I don't exactly know how I should set this. I have already set auth and my Authorization headers for both my single and parallel requests, and like I said, the single works because I can set $request->setAuth(). How can I set setAuth() for a parallel request, or is there something else I need to use?
EDIT Here is my code.
$client = new Client('https://service.example.com/');
$username = 'username';
$password = 'password';
$auth = base64_encode($username . ":" . $password);
$dm = $_POST['domains'];
$domains = explode("\n", $dm);
$client->setDefaultOption('auth', array($username, $password, 'Any'));
$options = array(
'query' => array(
'unit'=> 'y',
'period' => $_POST['period'],
'password' => urlencode($_POST['password']),
'registrant' => 'abcdefg-00004'
),
'headers' => array(
'Authorization' => 'Basic '.$auth,
'Accept' =>'application/hal+json',
'Access-Control-Allow-Origin' => '*'
),
'auth' => array($username, $password, 'Basic'),
'timeout' => '30',
'connect_timeout' => '10',
'cookies' => array('cert_url' => 'https://url.tocert.com/cert/su82jhdea3df5e81cd2cs8ejrae621b3a475312vd'),
'verify' => true,
'debug' => true,
);
$requests = array();
foreach($domains as $d){
$requests[] = $client->post($_POST['server-name'].'/domain?method=POST&callback=?&name='.$d, array(), $options);
}
//Do it
try {
$bulk_create = $client->send($requests);
foreach($bulk_create as $create){
echo $create->getBody();
}
} catch (MultiTransferException $e) {
echo "The following exceptions were encountered:\n";
foreach ($e as $exception) {
echo $exception->getMessage() . "\n";
}
echo "The following requests failed:\n";
foreach ($e->getFailedRequests() as $request) {
echo $request . "\n\n";
}
echo "The following requests succeeded:\n";
foreach ($e->getSuccessfulRequests() as $request) {
echo $request . "\n\n";
}
}

Facebook api post via "my application name"

I have facebook application that posts to its users timeline as status update. I am using the code below to initiate and post the message to all users, but my issue is the post "via name" on all mobile devices (iphone & BB, Samsung).The post appears on feed wall as if the user posted it himself not the application.
<?PHP
require_once '../scripts/config.php';
require_once '../inc/facebook.php';
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => 'true',
));
$dbh = new PDO('mysql:dbname='.$db_name.';host='.$db_host.';charset=utf8', $db_username, $db_password );
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlid = "SELECT SQL_CALC_FOUND_ROWS * FROM offline_access_users";
$sqlmsg = "SELECT message FROM fb_messages WHERE msg_id = (SELECT MAX(msg_id) FROM fb_messages WHERE sent = 'No')";
try {
$userid = $dbh->prepare($sqlid);
$userid->execute();
$id = $userid->fetchAll(PDO::FETCH_COLUMN, 1);
$msg = $dbh->prepare($sqlmsg);
$msg->execute();
$txt = $msg->fetchColumn();
}
catch(PDOException $e)
{
echo $e->getMessage();
die();
}
$counter = 0;
foreach($id as $fbid){
$counter++;
$body = array(
'access_token' => $access_token,
'app_id' => $fb_app_id,
'from' => $fb_app_id,
'message' => $txt,
'display' => "touch",
'redirect_uri' => "http://mydomain.com",
'link' => "",
'picture' => "",
'name' => "",
'caption' => "",
'description' => "",
);
$batchPost=array();
$batchPost[] = array('method' => 'POST', 'relative_url' => "/".$fbid."/feed", 'body' => http_build_query($body));
try {
$multiPostResponse = $facebook->api('?batch='.urlencode(json_encode($batchPost)), 'POST');
$poststs = "Sent Successfully";
}
catch(FacebookApiException $e){
echo $e->getMessage();
error_log($e);
die();
}
}
unset($batchPost);
$dbh = null;
What $access_token are you using? where did the you generate the $access_token?Which $access_token are you using?
The creator of the post (the via...) is whoever created that $access_token - the "from" will always be the owner of the Facebook account regardless of what you place there.

Categories