Json post method in swfit 3 for the below php code - ios

I have a PHP code that used to add user values to DB when the registration time. I need the swift codes to insert the values from the user like username, email, password to the data base. I did this once but that PHP code used POST method. How to post for this format.
<?php
function userReg($json_request){
//DB connection details
include 'connection.php';
$serviceId = $json_request['requestHeader']['serviceId'];
$fullname = $json_request['requestInput']['full name'];
$emailId = $json_request['requestInput']['email'];
$password = $json_request['requestInput']['password'];
$queryUser = "SELECT * FROM user_master WHERE email = '".$emailId."'";
$result_user = $conn->query($queryUser);
if($result_user->num_rows == 0){
$insertUserSql = "INSERT INTO user_master (user_name, email, password) VALUES ('".$fullname."', '".$emailId."', '".$password."')";
if (mysqli_query($conn, $insertUserSql)){
$getUserIdSql = "SELECT user_id FROM user_master WHERE email = '".$emailId."'";
$result_userId = $conn->query($getUserIdSql);
while($row_user = $result_userId->fetch_assoc()) {
$user_details[] = $row_user;
}
$userId = $user_details["0"]["user_id"];
$res['responseHeader']['serviceId'] = $serviceId;
$res['responseHeader']['status'] = "100";
$res['responseHeader']['message'] = "Success";
$res['registerUserOutput']['userID'] = $userId;
$res['registerUserOutput']['userInfo']['fullName'] = $fullname;
$res['registerUserOutput']['userInfo']['email'] = $emailId;
$res['registerUserOutput']['userInfo']['profilePic'] = "";
$json_user_output = json_encode($res, JSON_UNESCAPED_SLASHES);
echo $json_user_output;
}
}
else{
$res['responseHeader']['serviceId'] = $serviceId;
$res['responseHeader']['status'] = "99";
$res['responseHeader']['message'] = "Email ID already exists";
$res['registerUserOutput'] = "{}";
$json_user_output = json_encode($res, JSON_UNESCAPED_SLASHES);
echo $json_user_output;
}
}
?>

This answer covers the how to add parameters to a post request portion of URL session: POST Request in swift with key-value coding
Now I'd like to actually comment a bit on your PHP code. I see 2 problems with the way you do your SQL.
First I'd recommend using PDO instead ( helpful blog post here: PDO vs. MySQLi
Second and most importantly, you're directly interpolating your user generated values into your queries making you directly vulnerable to a SQL Injection attack.
The correct way to solve this would be to use prepared statements with PDO. Although you can use mysqli_real_escape_string() it is in general much easier and better to just use prepared statements. As these escaping functions ( most notably addslashes() ) can have vulnerabilities of their own and make your code a lot less readable than prepared statements do.
This would go something like this:
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);
Source: PDO Prepared statements
I haven't done any raw PHP in a while so this is as far as it goes for me, but should help you on your way.

Related

cl_http_utility not normalizing my url. Why?

Via an enterpreise service consumer I connect to a webservice, which returns me some data, and also url's.
However, I tried all methods of the mentioned class above and NO METHOD seems to convert the unicode-characters inside my url into the proper readable characters.... ( in this case '=' and ';' ) ...
The only method, which runs properly is "is_valid_url", which returns false, when I pass url's like this:
http://not_publish-workflow-dev.hq.not_publish.com/lc/content/forms/af/not_publish/request-datson-internal/v01/request-datson-internal.html?taskId\u003d105862\u0026wcmmode\u003ddisabled
What am I missing?
It seems that this format is for json values. Usually = and & don't need to be written with the \u prefix. To decode all \u characters, you may use this code:
DATA(json_value) = `http://not_publish-workflow-dev.hq.not_publish.com/lc`
&& `/content/forms/af/not_publish/request-datson-internal/v01`
&& `/request-datson-internal.html?taskId\u003d105862\u0026wcmmode\u003ddisabled`.
FIND ALL OCCURRENCES OF REGEX '\\u....' IN json_value RESULTS DATA(matches).
SORT matches BY offset DESCENDING.
LOOP AT matches ASSIGNING FIELD-SYMBOL(<match>).
DATA hex2 TYPE x LENGTH 2.
hex2 = to_upper( substring( val = json_value+<match>-offset(<match>-length) off = 2 ) ).
DATA(uchar) = cl_abap_conv_in_ce=>uccp( hex2 ).
REPLACE SECTION OFFSET <match>-offset LENGTH <match>-length OF json_value WITH uchar.
ENDLOOP.
ASSERT json_value = `http://not_publish-workflow-dev.hq.not_publish.com/lc`
&& `/content/forms/af/not_publish/request-datson-internal/v01`
&& `/request-datson-internal.html?taskId=105862&wcmmode=disabled`.
I hate to answer my own questions, but anyway, I found an own solution, via manually replacing those unicodes. It is similar to Sandra's idea, but able to convert ANY unicode.
I share it here, just in case, any person might also need it.
DATA: lt_res_tab TYPE match_result_tab.
DATA(valid_url) = url.
FIND ALL OCCURRENCES OF REGEX '\\u.{4}' IN valid_url RESULTS lt_res_tab.
WHILE lines( lt_res_tab ) > 0.
DATA(match) = substring( val = valid_url off = lt_res_tab[ 1 ]-offset len = lt_res_tab[ 1 ]-length ).
DATA(hex_unicode) = to_upper( match+2 ).
DATA(char) = cl_abap_conv_in_ce=>uccp( uccp = hex_unicode ).
valid_url = replace( val = valid_url off = lt_res_tab[ 1 ]-offset len = lt_res_tab[ 1 ]-length with = char ).
FIND ALL OCCURRENCES OF REGEX '\\u.{4}' IN valid_url RESULTS lt_res_tab.
ENDWHILE.
WRITE / url.
WRITE / valid_url.

Mysql loop return incorrect data

Good Afternoon All,
I am facing an issue and cant figure out what I am doing wrong in my myslq/php while/foreach loop.
Loop seems to be duplicating results.
////////////////////////Check which site have this app///////////////////
$query_t = "SELECT * FROM site WHERE app_id='2'";
$result_t = mysql_query($query_t) or die(mysql_error());
$rows = array();
while($r_t = mysql_fetch_array($result_t))
$rows[] = $r_t;
foreach($rows as $r_t){
$this_areport_site_id = $r_t['site_id'];
//////////////Search for user emails that have access to this app ////////
$query_t4 = "SELECT mail FROM $user_tbl WHERE arep_kitchen='1' AND site_id='$this_areport_site_id' ORDER BY id ASC";
$result_t4 = mysql_query($query_t4);
while ($r_t4 = mysql_fetch_array($result_t4)) {
$areport_kitchen_email .= $r_t4[mail].',';// append comma after each value you append to the string
}
echo 'Here: '.$this_areport_site_id.' - '.$areport_kitchen_email.'<br />';
}
Now it does return me following:
Here: AHROW - person1#email.com,
Here: AHROW - person1#email.com,person2#email.com,
Here: AHALEX - person1#email.com,person2#email.com,person3#email.com,
And I was expecting
Here: AHROW - person1#email.com,person2#email.com,
Here: AHLANG - No Records here
Here: AHALEX - person3#email.com,
I would appreciate suggestion what I am doing wrong there as I am sitting on this whole morning.
You should empty the string $areport_kitchen_email before appending emails to it:
$areport_kitchen_email = ''; // empty emails container string
while ($r_t4 = mysql_fetch_array($result_t4)) {
$areport_kitchen_email .= $r_t4[mail].',';// append comma after each value you append to the string
}
This will avoid duplicates.
I have fixed the issue by doing two loops one inside the other and resigning from comma separated string which seems to be an issue in first post.
Bozzy, thank you for an effort.
$query_t7 = "SELECT * FROM site WHERE app_id='2'";
$result_t7 = mysql_query($query_t7) or die(mysql_error());
while($r_t7 = mysql_fetch_array($result_t7)) {
$this_areport_site_id = $r_t7['site_id'];
$query_t8 = "SELECT * FROM admins WHERE arep_kitchen='1' AND site_id='$this_areport_site_id' ORDER BY id ASC";
$result_t8 = mysql_query($query_t8) or die(mysql_error());
while($r_t8 = mysql_fetch_array($result_t8)) {
$areport_kitchen_email = $r_t8['mail'];
}
}

Twitter get followers/ids curosr not working

I am trying to get the complete list of follower IDs for a twitter user. This user has 170,000+ followers. I am using the code bird library. Here's the code:
\Codebird\Codebird::setConsumerKey(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
$cb = \Codebird\Codebird::getInstance();
$cb->setToken(OAUTH_TOKEN, OAUTH_SECRET);
$cb->setReturnFormat(CODEBIRD_RETURNFORMAT_ARRAY);
$next_cursor = -1;
while ($next_cursor) {
$results = $cb->followers_ids("screen_name=some_tw_name","count=5000","cursor=$next_cursor");
$next_cursor = $results['next_cursor_str'];
}
The first call returns 5000 IDs, as it's supposed to. Subsequent calls (through the while loop) return 5000 IDs, but they are almost all the same as the previous call (each new call I get 1 to about 10 news IDs, with the remainder the same as before).
Anyone else run into this issue? resolution?
I think the problem is that you're not really combining the results from each of the requests. so you could do something like this:
function getFollowers(){
$next_cursor = -1;
$output = array();
while ($next_cursor) {
$results = $cb->followers_ids(array("screen_name"=>"some_tw_name","count"=>5000,"cursor"=>$next_cursor));
if(!isset($results['errors'])){
foreach($results['ids'] as $id){
$output[]=$id;
}
}
$next_cursor = $results['next_cursor_str'];
}
return $output;
}
Note: I also changed the argument notation to an array as opposed to strings.

Google Apps Script

I've created a simple script that reads through an xml file and posts the results to an SQL database. This works perfectly.
I've put a little if statement in the script to identify orders that have already been posted to SQL. Basically if the transactionID in the input array is higher than the highest transactionID on the SQL server it adds the row values to the output array.
It seems that I am missing a trick here because I am getting "TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")" when trying to compare the current xml row to the last transaction ID.
I've done some searching and whilst I can see people with similar problems the explanations don't make a whole lot of sense to me.
Anyway, here is the relevant part of the code. Note that this all works perfectly without the if() bit.
function getXML() {
var id = lastTransactionID();
var xmlSite = UrlFetchApp.fetch("https://api.eveonline.com/corp/WalletTransactions.xml.aspx?KeyID=1111&vCode=1111&accountKey=1001").getContentText();
var xmlDoc = XmlService.parse(xmlSite);
var root = xmlDoc.getRootElement();
var row = new Array();
row = root.getChild("result").getChild("rowset").getChildren("row");
var output = new Array();
var i = 0;
for (j=0;i<row.length;j++){
if(row[j].getAttribute("transactionID").getValue()>id){ //Produces: TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")
output[i] = new Array();
output[i][0] = row[j].getAttribute("transactionDateTime").getValue();
output[i][1] = row[j].getAttribute("transactionID").getValue();
output[i][2] = row[j].getAttribute("quantity").getValue();
output[i][3] = row[j].getAttribute("typeName").getValue();
output[i][4] = row[j].getAttribute("typeID").getValue();
output[i][5] = row[j].getAttribute("price").getValue();
output[i][6] = row[j].getAttribute("clientID").getValue();
output[i][7] = row[j].getAttribute("clientName").getValue();
output[i][8] = row[j].getAttribute("stationID").getValue();
output[i][9] = row[j].getAttribute("stationName").getValue();
output[i][10] = row[j].getAttribute("transactionType").getValue();
output[i][11] = row[j].getAttribute("transactionFor").getValue();
output[i][12] = row[j].getAttribute("journalTransactionID").getValue();
output[i][13] = row[j].getAttribute("clientTypeID").getValue();
i++;
}
}
insert(output,output.length);
}
I have seen my mistake and corrected.
Mistake was in the for loop.
for (j=0;i

RightFax 10.0 Integration With C#

If I insert the values into the corresponding tables of RightFax, does it FAX automatically or do i need to write the following code for that?
RFCOMAPILib.FaxServerClass faxserver = new RFCOMAPILib.FaxServerClass();
faxserver.ServerName = "ServerName";
faxserver.Protocol = RFCOMAPILib.CommunicationProtocolType.cpNamedPipes;
faxserver.UseNTAuthentication = RFCOMAPILib.BoolType.True;
faxserver.OpenServer();
RFCOMAPILib.Fax fax = (RFCOMAPILib.Fax) faxserver.get_CreateObject(RFCOMAPILib.CreateObjectType.coFax);
// set up your 'fax' object the way you want it, below is just some sample options
fax.ToName = "John Doe";
fax.ToFaxNumber = "4255551111";
fax.ToVoiceNumber = "4255550000";
fax.ToCompany = "ACME";
fax.FromName = "My Company";
fax.FromVoiceNumber = "4255552222";
fax.Send();
Can you please provide me sample code for attachments? If RightFax sends the FAX automatically then which tables do I need to fill-in in order to do that?
Thanks
the below code is vb.net but will show you how to send a fax and add attachment as well, hope this helps. I've written a post about this here but here's the code below ....
Dim FaxAPI As RFCOMAPILib.FaxServer
Dim fFax As RFCOMAPILib.Fax
FaxAPI = New RFCOMAPILib.FaxServer
FaxAPI.ServerName =something
FaxAPI.Protocol = RFCOMAPILib.CommunicationProtocolType.cpTCPIP
FaxAPI.UseNTAuthentication = RFCOMAPILib.BoolType.False
FaxAPI.AuthorizationUserID = something
FaxAPI.AuthorizationUserPassword = something
FaxAPI.OpenServer()
fFax = FaxAPI.CreateObject(RFCOMAPILib.CreateObjectType.coFax)
fFax.Attachments.Add(“D:\FilePickupIn\2222222222-20110322142718-01.tif”)
fFax.HasCoversheet = RFCOMAPILib.BoolType.False
fFax.ToFaxNumber = something
fFax.ToName = something
fFax.Send()
FaxAPI.CloseServer()
FaxAPI = Nothing
fFax = Nothing
happy to help if you get stuck with anything...

Resources