PHP Web Service - php

I am new to the world of PHP and coding as those have helped me answer a number of questions I have posted know. I have the opportunity to jump into the deep end however seeing I have no clue about PHP and web services, I am seeking your help. I would appreciate it if you could point me in the right direction as I have not been able to find any examples nor detailed information regarding it.
Effectively there is a PHP page that needs to pass certain values to a .NET web service i.e. name, email, mobile number and upon the receipt of such information the web service sends out a text message. This is information I have so far
The Allocation service uses a basic HTTP binding and TransportWithMessageCredential security.
Sample C# code using a reference generated with Visual Studio tooling
Allocation.AllocationServiceV3Client Client = new Allocation.AllocationServiceV3Client();
Allocation.AllocateResult Result;
string VoucherCode;
DateTime ExpiryDate;
string DisplayMessageText;
Client.ClientCredentials.UserName.UserName = ? ;
Client.ClientCredentials.UserName.Password = ? ;
Result = Client.AllocateToConsumerMobileAndSendSMS(out VoucherCode,
out ExpiryDate,
out DisplayMessageText,
ClientID,
ClientReference,
CampaignID,
ActivityID,
MobileNumber);
if (Result != Allocation.AllocateResult.Success)
{
}

I have no clue about PHP and web services
First things first, pick your brain up out of the C# soup and sit it out to dry off for a few hours. PHP is a simple language for simple things. If you're new to it, you don't want to try and make it do complex things, and you certainly don't want to try writing C# style code in PHP.
Let's take "web services" as an example of a "complex" thing. In some circles, this means horribly designed monstrosities like SOAP. VS makes it relatively easy to build SOAP bindings to your existing classes, so it's not a painful solution for you.
SOAP is a very painful solution for PHP.
Effectively there is a PHP page that needs to pass certain values to a .NET web service
It sounds like you have an existing web service set up then, correct?
You could try and use the built in SOAP client, but it's an undebuggable and unconfigurable binary blob of horrors that will make you want to kill someone if it doesn't work for you immediately.
Edit: The section below was written incorrectly assuming that both sides were under your control. As this is not the case, you can disregard the rest of the answer. I'm leaving it up because it may be valuable to others.
I would advise a different approach. Set up an endpoint (URL) in your .NET application that expects the data you've specified as POST values, and have PHP POST the data. This has the distinct advantage of being incredibly simple. Because you're new to PHP, simple is a big win.
(In other words, ditch the "web service" -- or make a parallel copy if other things consume that service already.)
PHP comes with a service called PEAR, a repository of helpful classes. You can use HTTP_Request2 to quickly build the HTTP POST request. Chances are that it's already installed. If it isn't, it's easy to install either at the system level using the pear command, or locally in your project.
Here's some quick example code copied straight out of the reference guide:
$request = new HTTP_Request2('http://www.example.com/your/endpoint/url.foo');
$request->setMethod(HTTP_Request2::METHOD_POST)
->addPostParameter('name', '...')
->addPostParameter('email', '...')
->addPostParameter('mobile', '...');
$response = $request->send();
You can then read the response as needed.
Now, this isn't a perfect solution. Your C# code mentions a username and password, which I suppose could be included in the POST data. You could also use HTTP authentication (supported by HTTP_Request2) instead.

Ok, now from what I understand, C# will make a request to the php page, wich then (in simple html) gives it back some data.
PHP Is made to do some calculation serverside and when it's done it will send the prints and echo's to the client side as html (simple explanation)
If you want to start using php you would be best off getting into the basics by following the tutorials at W3School (http://w3schools.com/php/default.asp)
Then you can use http://php.net as a reference for more functions you might need.
The way you'll want to set it up is for C# to make a request to a page like action.php
And then using a $_GET method to get the data so C# would send a request to
action.php?userid='456'

Related

Create plot from database server side and visualize it on client side

I'm currently trying to find the best way of doing this:
We have a python program (client side) we use to upload metrics on a mySQL database on a server and later, via web check it and filter it, etc.
The problem arises when we try to plot any query from to the database. It's unclear to us what aproach to use. The main page was made with Joomla in php.
Currently I was looking into python alternatives to run on the server side, somehow capture the query, process the data, create the image and then return it to the client side as an image or as a string to be reconstructed on the client side. But as I have read it seems also possible (and maybe easier) to do the same in PHP or JavaScript which (as I understand it) run on the client side, leaving less to worry about.
Is it that so? Are my assumptions right? Which aproach would you use/pŕefear? Is there some link or info you could give me to continue my search?
I would prefer to do it on Python using something like matplotlib, plotly, bokeh, etc. but as I see it, the problem is not about creating the image, but about comunicating and sending the image information between server and client.
Thanks!
I think it possible to do what you want to with bokeh. I dont know joomla but it think its not that important.
Check out that part of the bokeh documentation Embedding Bockeh
then for the server parts in python i would recommend flask to start with.
You can just prototype your app with one of the bokeh server apps examples from the bokeh repo.. there are flask examples too.
Then you can start to extend the bokeh server app from the examples with a query to your database with sqlalchemy or
mysql-flask

Telegram-based chat on a PHP-based site: HOWTO?

I can't figure out what exactly to use for interaction between my site and the Telegram service (first of all - how to get the authentication process done using PHP and other stuff like chat among users).
On this page: https://core.telegram.org/api I haven't got an idea how to use those functions in PHP.
According to this page: https://telegram.org/apps
I have two choices:
1) The CLI-interface (unofficial, by the way): https://github.com/vysheng/tg
and it doesn't have an autentification function among others. In order to authenticate yourself, you need to run:
bin/telegram-cli -k tg-server.pub
and inside of the application you have to enter your cell phone and the secret code sent by SMS - after that you're authorized. Then you install https://github.com/zyberspace/php-telegram-cli-client and run telegram-cli as a daemon:
./bin/telegram-cli -dWS /tmp/tg.sck -k tg-server.pub &
Does it mean that I have to create tg-server.pub manually using PHP for each user which is trying to login?
2) Webogram: https://github.com/zhukov/webogram - but it's written on JavaScript and has very complicated code.
Dear Stackoverflow gurus, maybe you're more attentive than I am and could help me to recognize the right solution (or example, I don't know, the PHP snippet or anything else) for the user's chat based on the Telegram and PHP?
I would greatly appreciate it!
Thank you!
I have posted a step by step guide on getting your AuthKey (VB.net) here
The main challenge with Telegram API is the documentation... but if you can work through the first part - getting an AuthKey then i believe the rest should fall in place ... with some more effort.
Working through some GitHub src might be time consuming, it might be best to get a handle on the documentation and then work your way to building your own code for TelegramAPI from scratch
Most likely, PHP wrapper for Telegram API doesn't exist. I'd wager it's because communicating with Telegram servers from your server-side PHP code defeats both of the core features of Telegram: speed and security.
no speed - a message has to hop through one additional loop (your server) before it reaches the recipient.
no security - browser page will communicate with your server via AJAX or forms, I assume. This means sending data as plain text (unless you're on https), open to the whole world to see (if you were to sit on a public wifi, or something like that).
You can implement the Telegram API, it's a bit involved, but doable. But it's totally pointless, in my opinion.
As an alternative, just embed the webogram in an <iframe> or something :)
Now, you can use MadelineProto https://github.com/danog/MadelineProto - enought powerful PHP client for MTProto Telegram!

PHP: REST versus Functions

Ok, this is probably something answered millions of times, but I couldn't find an answer on Google (maybe I'm using the wrong parameters?).
Here is the thing:
I'm planning to implement a REST webservice using PHP. This webservice is supposed to serve a mobile app and also a website (located on the same server/virtualhost/whatever).
For example, URLs will be something like:
http://www.somedomain.com/API/ (RESTful webservice)
http://www.somedomain.com/ (WebSite, which will fetch data from the same sources that the webservice uses).
Now my question is:
Which type of query is the most recommended for querying the database? For example, if I login from the website (http://www.somedomain.com/), it would be better to implement another internal PHP API for that or to login using cURL to the REST API and why?
I know, if I implement another PHP function for login from the website, I will be using one less HTTP connection to my server, but wouldn't it break the idea of the RESTful API?
Thank you in advance.
I'd say you want to leave out the HTTP round-trip where you can. That's potentially extremely wasteful, though if the calls are rare then it may be worth it for the code de-deduplication. It all depends on your application really.
Don't worry too much about "breaking the RESTful API" — by your logic, your own application should have no function calls and just be full of curl invocations to itself. No, instead, at some point, your code needs to shake off the management speak and just get down to bloody work. :)
I am sure most of you know this, but for the sake of completeness:
A function or procedure is a designated program-section that handles a specific task and usually consists out of a function declaration or definition and a function body.
When a function is called remotely, it is known as a remote procedure call (RPC), and the invocation implementation as a Remote Procedure, which is generally handled by an RPC- API and in rare cases by an ABI.
So at the heart of your quest lies questioning the necessity for remote procedure invocation, and how much a given procedure should perform. As such heeding good code-refactoring guidlines is a good start to a good API. I generally adhere to the following pointers:
If you do not need the necessity of remote invocation for a given procedure, do not expose it unnecessarily.
Choose wisely which procedures you make API accessible and unit-test them.
Create various wrapper functions which successively call a set of local functions to perform specific tasks as opposed to calling each function remotely, and render them accessible to the REST API.
An initial authentication procedure is a good idea, but I would recommend using established protocols such as OAuth 2 rather than implementing your own. As such may end up using curl in your remote php script.
This should get you started.
Other than that you do not provide enough information to give you a more specific answer. I would generally recommend to look at google, yahoo, facebook, and perhaps NCBI's PUG to see how they implement their REST APIs, which are often a good case-study.

Webservices and PHP

The small company i work for have asked me to MAYBE write a webservice in PHP. I have used webservices (Flickr, Youtube and so on). But never written one. I am an experienced PHP webdeveloper (obviously not webservices).
Before a accept/deny this task. Are there anything special with webservices or can i just do i like this:
if ($GET["something"]==="somethingelse")
{
header('Content-Type: application/json');
echo json_encode(array(Answer => 'yes'));
}
Can i do it like that?
Yes, you can, in fact this is a good start for your first webservice i think.
Having a php-script with diffrent branches (if-tests on url parameters) is very fast and easy way to create a webservice.
I would not use a SOAP/XML based webservice unless it was strictly required. (Saves time writing WSDL, testing etc). SOAP/XML webservices may give you features like UDDI (service catalogue), but again, I wouldn't waste time make it if it was not required :-)
I have found nuSOAP to be rather comprehensive and easy to use.
Please check out their page and ask again, if you run into difficulties
http://sourceforge.net/projects/nusoap/

Server setup for basic forum-style app on iPhone?

I'm not really a server-side person – I generally do iPhone apps, though I've hacked together a few Wordpress sites.
I'm curious as to what web technologies people would use for the back-end of an iPhone app whose front end presents as a basic forum. In other words, people can create new threads, and respond to them - with plain text only.
The forum would not exist as a website.. the only way to access it would be on the phone.
What technology would people recommend I use? Ruby-on-Rails with Amazon S3 storage? Could I even use existing forum software and pass and receive data to and from it? Perhaps even a forum Wordpress plug-in? Or is there a better way?
If you wanted to, you could use existing forum software and/or Wordpress to facilitate what you want, which would be easier than building your own forum from scratch. You could, with that existing framework, set up your own little API to communicate from the iPhone app to the server- for example, send a $_GET request to a PHP script on your server, which would return a list of forum topics. You could have similar PHP scripts that could do similiar functions, like adding a post or deleting topics.
That's pretty much how I've got it set up on an iPhone app I recently made- my server has a basic forum system, and I just wrote a couple of PHP scripts to return information from a MySQL server. However, if you'd particularly prefer to use Wordpress/Amazon S3/whatever else, then I could give more specific instructions relating to those services.
*EDIT*
Here's an example PHP script you could use (after you've created databases):
forumcategories.php
<?php
// insert database connecting logic here
$query = mysql_query("SELECT * from categories");
echo "<categories">;
while($row=mysql_fetch_array($query)){
echo "<category><id>" . $row['id'] . "</id><title>" . $row['title']; . "</title></category>;"
}
echo "</categories>";
?>
This is a really simple script- of course, you would need to add in code to connect to the database (which can be found easily online) and probably some error checking, but other than that, it will do the trick. What you would do in the iPhone app is send a request to http://yourserver/forumcategories.php and it would return XML listing all of the categories, which can easily be parsed with NSXMLParser and placed into a UITableView, for example.
Google App Engine is very good for what you describe. There are a lot of advantages to this approach: choice between Java and Python, access to the Google Accounts API, persistence/datastore APIs,... and you don't have to setup much to start working.
I also recommend that your server app returns responses formatted according to Apple's XML Property List format, instead of any other XML or JSON format. You can avoid NSXMLParser (or any other parser) altogether and save time to use on other important stuff.

Categories