PHP ibase query - php

i want to use PHP for internet service in my IOS 7 application.
I've tried to generate an code for receiving objects from a Firebird database.
My Code bellow:
<?php
$host = 'xxx';
$username = 'xxx';
$password = 'xxx';
$conn = ibase_connect($host, $username, $password) or
die ("Verbindung fehlgeschlagen");
$arr = array();
$data = array();
$stmt = "select * from xxx";
$sth = ibase_query($conn, $stmt);
while ($row = ibase_fetch_object ($sth)) {
$data['ID'] = $row->ID;
$data['VON'] = $row->VON;
$data['AN'] = $row->AN;
$data['BETREFF'] = $row->BETREFF;
$data['DATUM'] = $row->DATUM;
$data['KOMMISSIONSNR'] = $row->KOMMISSIONSNR;
$data['NACHRICHT'] = $row->NACHRICHT;
$data['GELESEN'] = $row->GELESEN;
$data['GEANTWORTET'] = $row->GEANTWORTET;
$data['STATUS'] = $row->STATUS;
$data['AUFTRAG'] = $row->AUFTRAG;
$arr[] = array($data);
}
echo json_encode($arr);
?>
By calling the code in Safari-browser no values or arrays be returned.
If giving an Key like 1
(echo json_encode($arr[1]);) an array on the browser will be shown.
How can i show the whole array with all keys? I should add that i want to use the results from my ibase-query as NSDictionary in the application.
And which framework is the best for encoding the PHP-Array to IOS? I've tried to use JSONDictionaryExtensions, but don't know if it's the right for this Code.
i hope you can help me.
Sorry for my bad english.

I'm not sure why you are putting the $data array into another array. You can skip that step.
When you echo json_encode($data) you encode the array as JSON, so that is what your iOS application gets and what it needs to decode. Decoding JSON into an NSArray or NSDictionary has been asked many times here on StackOverflow, like this question.

Related

How to post and get data from the same php file [PHP]

I've php file and with this I want both set and get data. (Is it possible?).
This is my php file:
require_once 'GetAreaDB.php';
$latitudeFrom = $_POST['latitudeFrom'];
$longitudeFrom = $_POST['longitudeFrom'];
$maxDistance = $_POST['maxDistance'];
// These three values come from my swift app.
$getArea1 = new GetAreaDB();
$locals = $getArea1->getPositionByCoordinate($latitudeFrom,$longitudeFrom,$maxDistance);
$jsonArray = array();
foreach ($locals as $local) {
$jsonArray1 = array(
"id" => $local->getID(),
"json_local" => $local->getJSON()
);
array_push($jsonArray,$jsonArray1);
}
echo json_encode($jsonArray);
My idea is to create a php file that receives post parameters and then, on the base of these, it get other data.
How can I solve?

posting data from sqlite to mysqldb using json

I am trying to get JSON posted by an android application and decode it into an array to store it in my sqldb.
I have written the following php code. When I execute it, it displays [] (line 6).
<?php
include_once './db_functions.php';
//Create Object for DB_Functions class
$db = new DB_Functions();
//Get JSON posted by Android Application
$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : '');
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
//Based on insertion, create JSON response
if($res){
$b["id"] = $data[$i]->userId;
$b["status"] = 'yes';
array_push($a,$b);
}else{
$b["id"] = $data[$i]->userId;
$b["status"] = 'no';
array_push($a,$b);
}
}
//Post JSON response back to Android Application
echo json_encode($a);
?>

Passing Array via SOAP for SAP Webservice

I am trying to pass a 2D Array via SOAP for SAP Webservice.
But I am unable to pass the same.
I am able to pass a value, also I am able to accept a table output from SAP.
Please guide.
I tried to typecast array as an object.
My Code:
<?php
include("include/config.php");
$sql = "SELECT tid,OrderNumber FROM transhistory ORDER by timestamp ASC limit 2";
$result= mysqli_query($db,$sql);
$i=0;
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
//Array based on output table
$pmt[$i][0] = ""; //Mandt
$pmt[$i][1] = $row["tid"]; //Refnum
$pmt[$i][2] = $row["OrderNumber"]; //Orderno
$i++;
}
/*Two methods I tried */
$object = (object) $pmt;
$object = json_decode(json_encode($pmt), FALSE);
#Define Authentication
$SOAP_AUTH = array( 'login' => 'abc',
'password' => 'abc');
#Specify WSDL
$WSDL = "working URL here";
#Create Client Object, download and parse WSDL
$client = new SoapClient($WSDL,$SOAP_AUTH);
#Setup input parameters (SAP Likes to Capitalise the parameter names)
$params = array(
'Zpmt' => $object
);
#Call Operation (Function). Catch and display any errors
try {
$result = $client->ZphpOT($params);
} catch (SoapFault $exception) {
echo 'Error!Server Connectivity issue. Please try again later.';
die();
}
?>
I don't know if the answer may be useful for somebody but usually you send parameters like this
$result = $client->ZphpOT("VARIABLE_NAME"=>$params);

How do I access a JSON-property of a JSON-Object in php?

I've got a really weird problem and I can't figure out why.
The situation is quite simple. My Android app uploads JSON data to a php script on my server. Right now I am trying to parse the data.
This is the JSON-Array passed to the script (via httpPost.setEntity ()):
[{"friends_with_accepted":"false","friends_with_synced":"false","friends_with_second_id":"5","friends_with_first_id":"6"}]
This is the php script:
<?php
// array for JSON response
$response = array();
$json = file_get_contents ('php://input');
$jsonArray = json_decode ($json, true);
foreach ($jsonArray as $jsonObject) {
$firstId = $jsonObject['friends_with_first_id'];
$accepted = $jsonObject ['friends_with_accepted'];
$secondId = $jsonObject ['friends_with_second_id'];
$synced = $jsonObject ['friends_with_synced'];
echo "accepted: ".$accepted."synced: ".$synced;
} ?>
And this is the response I get from the script:
accepted: synced: false
Why is the "synced" property correctly passed, but not the "accepted" property??
I can't see the difference. Btw, firstId and secondId are parsed correctly as well.
Okay, i just found the problem:
Instead of
$accepted = $jsonObject ['friends_with_accepted'];
I deleted the space between jsonObject and the bracket
$accepted = $jsonObject['friends_with_accepted'];

How to send parameters via the URL to PHP from Flex HttpService

Forgive me if this has already been answered/ is extremely basic/ the question is worded incorrectly, I am very new to this and struggling.
Basically I have back end PHP which generates XML, the flash builder then inherits the data. Where I'm stuck is understanding how the flash builder can send a parameter to the PHP through an HttpService e.g
This is what it currently interprets:
http://..../file.php?action=getitems
What I would like the flash builder to send is
&class=fruit (<- the class would be dependant on what is selected from the drop down in the application)
to overall create this string
http://..../file.php?action=getitems&class=fruit
Thank you and apologies if this is nonsense. I'm using Flash Builder 4.
This is actually rather simple in Flex...
var service : HTTPService = new HTTPService();
service.url = "http://localhost/getData.php";
service.method = "POST";
var parameters:Object = new Object();
parameters["action"] = "getitems";
parameters["class"] = "fruit";
service.send(parameters);
... done!
Overall I would use the push method instead of passing a variable, lessens the chance of getting hacked from the middle.
My AS3 Code for the http call:
public function someRequest() : void
{
var service : HTTPService = new HTTPService();
service.url = "http://localhost/getData.php";
service.useProxy = false;
service.method = "POST";
service.contentType = "application/xml"; // Pass XML data.
service.request = "<ID>somevalue</ID>"; // The XML data.
service.resultFormat = "xml"; // Recieve XML data.
service.addEventListener(ResultEvent.RESULT, createFields);
service.addEventListener(FaultEvent.FAULT, handleFault);
service.send();
}
private function createFields(event : ResultEvent) : void
{
var result : String = event.result.toString();
returnData = XML(result);
}
private function handleFault(event : FaultEvent) : void
{
var faultstring : String = event.fault.faultString;
Alert.show(faultstring);
}
As you see toward the middle, there is an XML space for entering a variable. I use this approach to pass data back and forth from the PHP to the AS3.
The PHP is:
<?php
define("DATABASE_SERVER", "localhost");
define("DATABASE_USERNAME", "root");
define("DATABASE_PASSWORD", "**");
define("DATABASE_NAME", "dbName");
//connect to the database.
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
mysql_select_db(DATABASE_NAME);
$Query = "SELECT * from data WHERE employeeID = '" . ($_POST['ID']) . "'";
$Result = mysql_query($Query);
$Return = "<data>";
while ($User = mysql_fetch_object($Result))
{
$Return .= "<user><userid>" . $User->userid . "</userid><username>" . $User->username . "</username><emailaddress>" . $User->emailaddress . "</emailaddress></user>";
}
$Return .= "</data>";
mysql_free_result($Result);
print ($Return)
?>
Hope that helps you on your way.
I generally handle this through [POST] instead of [GET]
In your actionscript function:
private function sendRequest():void {
var obj:Object = new Object();
obj.action="getitems";
obj.class="fruit";
myService.send(obj);
Your httpService
<s:HTTPService id='myService' url='urlToYourPHP' method='POST' result='yourResultHandler' fault='yourFaultHandler' resultFormat='XML'/>
As powelljf3 said, POST is more secure then GET though it can still be gotten to.

Categories