symfony working with Web Service - php

I am working in symfony. I am retrivind data from a web service.
Currently I am using "Listener" to get data using web service. Is it wring way to do through listeners?
Is there any concept of Models in symfony to get data using web service? rather than calling web service from listener!

Actually a listener isn't something that would fit your case. You need a regular service, because listeners are supposed to react to Symfony domain events.
On creating services, you should read the official documentation (though if you've created an EventListener then most parts of the manual would be familiar to you).
If you're wrapping a foreign API then you should definitely do some research on whether the API is already wrapped (packagist.org is a good start: here's a wrapper for Twitter's API for example). If it's not, then it's up to you to pick an HTTP client to communicate with the service, and wrap its API into a PHP class that you would then expose in your service.
To understand how exactly you want to do this, try searching Packagist for Symfony bundles that wrap some APIs. Here's a Foursquare API bundle that uses an abstracted client library for example. Note that it depends on Guzzle HTTP client, and also take a look at the Guzzle Client class.
Also, here's Google's official API client for PHP. You could grab some ideas from there, too.

Related

Using external data in Laravel 5.5

I'm building a site that has to display data collected from an external web service. This service provides multiple data endpoints that are all authenticated against via OAuth.
Rather than each view having to hit an internal method to generate this data (and login to the external service each time) it seems like it'd be much better to create a singleton that will (lazy) connect to the data provider service when a view needing the data is first loaded, and then the various methods supporting the views could just call this same service provider to get the data.
Would this be best in the Laravel world as a service provider? As a helper class with a singleton? Or some other way?
Your Laravel application boots from scratch every time it receives a request (you can cache objects from a previous request, but not an instantiated service), so it won't be possible to create a service that keeps running authenticated between requests (at least with PHP).
It's not uncommon to create a service and authenticate once per request.
That being said, you can create a service and bind it to the container using $app->bind() or $app->singleton().
Docs about it here: https://laravel.com/docs/5.5/container

How to integrate python/django project with a webservice that provides api only for php?

I work on a django website. Currently, I need to integrate it with a third-party API that provides only PHP endpoints and some shitty documentation that instructs how to build PHP requests. The API provider introduces a PHP client containing a class that handles requests to PHP API webservice via SOAP.
First, is it possible to build python client that will interact with PHP endpoints without knowing server-side trickery of the API provider? Should I dive deeper into PHP client source code and try to rewrite it in python?
Second, should I create intermediary PHP webservice that will integrate third-party PHP API and provide a precise API to interact with python client.
Is there any better option?
P.S. Please, note that I'm new to PHP and SOAP.
Just like any other API. You just have to use the PHP client as a guide to build the API client in python. If you feel it's no help at all, throw it away and look what the endpoints are and what they spit out.
In the end, the endpoints on the remote server can be programmed in Smalltalk for you care. The API client only cares about what parameters to send to the server and what the response looks like.
So pick your basics:
requests for network protocol
soap client
authentication if oauth
and start coding ;) Good luck!

Web to Native app - Request & Response - Listening service

This is a very general question, however I have done some research and believe I am simply missing some useful terminology of the concepts in question, and if someone could point me in the right direction it would be helpful.
Basically I am creating a PHP page, that will send a request - this will then be received by a native app. Where the user will either accept or decline the request. This will then be sent back to the PHP page.
So my queries..
What technologies or key concepts need to be applied here..
Main areas
-- PHP to send request
-- Native app to be listening for requests
-- PHP to be able to listen for the response
-- All needs to happen in real time i.e. user requests service, administrator confirms/declines, user receives response. All within in est. 60 seconds.
If you want to go into the right direction and you want all this to be on PHP, I would look at PHP RESTful service. Maybe
read up on this resource: http://www.amazon.com/RESTful-PHP-Services-Technologies-Solutions-ebook/dp/B005VQ8SB6/ref=sr_1_1?s=books&ie=UTF8&qid=1419012976&sr=1-1&keywords=9781847195531
You can also look into Symphony or Zend frameworks, there are other frameworks that has similar feature. If you need this quick, you can get it installed and test out what you're looking to accomplish.
Also take a look at Symphony snippet on Request & Response - Listening service
http://php-and-symfony.matthiasnoback.nl/2011/10/symfony2-how-to-create-a-custom-response-using-an-event-listener/
Something on previous post from Stackoverflow
symfony provides a solid framework for HTTP and restful API through which jQuery mobile can be configured and then if you extend TWIG for a template, then Symfony acts as the bundler and composer through with routing can take place. While Symfony does not run native apps, they need to retrieve and store data and on that front a framework such as Symfony works well. So one could argue that one could use APIs for multiple clients (iOS, Android and more) and then reuse controllers and use formats to generate XML/JSON thus theoretically it should work with Phonegap too.
#ref: Creating mobile app in Symfony2

Events in PHP slim web services

I am writing a web service in PHP using the Slim framework. I have a web application and a mobile application which uses the web service. My web application posts some data to the web service and web service writes it to a database. But I also wanted the web service to send a notification to my mobile application.
I assume this is some kind of an event based action. How to perform this for PHP slim web services? Or in general, for a PHP web service?
I personally use the symfony2 EventDispatcher component to achieve what you speak of. It's a clean way to, well, implement an event-based architecture. This particular implementation of the mediator pattern can make your code extensible with minimal overhead (and much organization).
Link: http://symfony.com/doc/current/components/event_dispatcher/introduction.html
Note: The EventDispatcher can be used independently of symfony2. The above link contains the packagist link of the component.
Edit: If you are a fan of global variables, you could look into the do_action function in WordPress.
I think there is no easy way to implement a direct event notification from Slim to a mobile device. To do such thing you need to open a socket between device and server so you can send direct notification to the device.
With Slim, the easiest way to achieve what you want is to use the list/queue pattern and tell the device to periodically ping your web service (like every 30 sec) and check if there is something new in the queue.

Access a webservice from a BlackBerry app

I have no idea how to access a PHP based WebService from a BlackBerry.
Can anybody guide me in accessing one?
Your options are either use a proper API interface as suggested by snowflake (like REST, JSON/XML RPC etc.) or to scrape the HTML (very painful -- especially on mobile devices). The language in which the service is implemented shouldn't matter. If you are targeting OS6, there is a new communication API that makes consuming webservices easier (A sample can be found at http://devblog.blackberry.com/2011/01/communication-api-2/), but it's not too difficult on OS5 or below either. If you need to use JSON in OS5, your can find the org.me.json package online and use that.
If the webservice is under your control, you should add RPC support, the difficulty of which depends on the framework (if any) you're using for your project. Your question's a little scant on that information, so I can't provide any specific details.
See this Web Services in BlackBerry J2ME Application article. Here clearly described how to use Web services in Blackberry. And also you can refer Web services overview documentation.

Categories