How to use soap in javascript - php

I am trying to use SOAP in javascript but i am not getting how to start it.
Here is the code sample i write in PHP and it works fine. I want to write this code in Javascript. In following code i call one api from a www.example.com and for calling certain api we require to pass some parameters.
$soapClient = new SoapClient("https://www.example.com/WSDL");
$param_sh = array( );
$header = new SoapHeader('http://somesite.com/action/', 'user_credential', $param_sh);
$soapClient->__setSoapHeaders(array($header))
$param = array("with some parameter");
$contents = $soapClient->__call("name_of_method",array($param));
print($contents);

Now, after more than 5 years since this question was asked, lots of stuff has happened to Javascript and web in general, so:
you can send cross-origin requests and modern browsers are 100% OK with that (if the server you're sending them to allows CORS, of course)
there are lots of libraries both for server and client side javascript.
there are lots of questions on the topic on StackOverflow.

Assuming you're talking about in-browser Javascript, you won't be able to use SOAP. Browsers obey something called the Same Origin Policy, which says (loosely) that you can't make cross domain requests from javascript. That means you can only make requests to a SOAP service (or any HTTP request) if it's on the same domain that the browser page is currently on. Because of this limitation/feature, no one ever went to a lot of trouble to implement a SOAP client in Javascript (although they probably do exist).
Your best bet is to make your SOAP calls via PHP, and then make AJAX requests from your web page (via javascript) to the PHP pages that make the actual SOAP request.

JavaScript doesn't have a SOAP library out of the box, though you can google around and find them, e.g., here. Not that this won't do anything to work around the limitations #Alan cites. The web services still need to be on the origin server.

Related

I have implemented Adobe Analytic for my client. I read the response from network panel of browser through php

I want to fetch the response from adobe analytics which I get in network panel of browser and display in it the page. This there any way to read the response through php. I don't have access to core files. i work as third party implementation team.
The short answer is you can't. php is server-side. Assuming you did the standard javascript Adobe Analytics implementation; well that's client-side. Your server can't see that stuff.
The longer answer is redeploying Adobe Analytics through a server-side implementation. Basically you setup the code but have it point to your server and then you forward it to adobe (proxying), and so now it is exposed to you server-side.
The alternative answer is depending on what it is you are actually trying to accomplish, you can make use of Adobe Analytics' s.registerPostTrackCallback function. Basically it lets you register a callback function to be called after every s.t or s.tl call, and it gives you the full/final request URL sent to the AA collection server. You can then make an AJAX request to pass it to your server and do whatever with it. Or since you mentioned displaying it on page, maybe consider using javascript to render it on the page? But if you're looking for actual response stuff (headers, content) well you're out of luck on this option.
The other other alternative answer is.. this almost sounds like you are looking to make some kind of browser plugin? If so, then on a plugin/extension level, the request/response stuff (including header stuff) is exposed on that level. But again, ultimately this is really a client-side solution..
But first step back and more clearly define what it is you are trying to do. Or if you've done that already, then try (more clearly) to convey that here.

Javascript to PHP rather then using Ajax?

I need to call a PHP Script and Run (a function) on the Server .. by calling from client side by using Javascript. I know only Ajax Call from Javascript.
Is there any other standardized way to communicate from Javascript to PHP?
Please correct me if i'm wroing. Is XMLRPC an another approach?
Nothing that is well supported or practical.
Ajax is just shorthand for "Making an HTTP request from JavaScript without leaving the page".
PHP is heavily geared towards being a server side web language (so it is optimised for being accessed over HTTP). Browsers are focused on accessing content over HTTP.
No, you use a XmlHttpRequest (that is, I assume you don't want the user to experience any sort of page refresh).
To work cross browser easily I'd recommend using a library like jQuery which handles everything for you, everything is nicely encapsulated and abstracted so you don't need to worry about any of the details. That way calling your script becomes extremely easy.
XMLHttpRequest is the best way, as far as I know, but there are other techniques too. There is the old school way some sites still use. Using hidden iframes and sending request through it.You create an iframe with javascript, append it with 0 width and height and the request the php file. The output must be script that somehow communicates with the parent window script.

Should i use https request or ajax to send data to clients?

So i'm working on an A/B tester website, similar to http://www.optimizely.com/ and i'm quite new to web development. An A/B tester pretty much allows clients to create variants of their website to make optimizations based on user response (mouse clicks, etc.). So once the variants are made on our website (e.g. larger button size), my job is to send a package to the client which allows them to access and run the javascripts of the variants on the clients end when their page loads. Do i need an ajax call to send this data or can it be done via https request and what are the pros and cons for what i need done? (We're using mysql, hadoop and php). Thanks.
This question doesn't make a lot of sense to me.
HTTPS is a communication protocol. AJAX is a programming pattern (or, perhaps more cynically, buzzword). AJAX most often would use HTTPS to accomplish the actual secure communication between client and server.
If I understand correctly what you mean....
Depends of what data is being sent - if it is personal data, always use HTTPS calls (ie. request the data from https://yourdomain.com/your_script.php), otherwise HTTP (ie. http://yourdomain.com/your_script.php) will be ok (both of these can be done via ajax, so that's not a problem).

Simulating Browser Clicks In PHP

I want to write a PHP script that performs a routine task in a web app I use. I am trying to figure out the easiest way to submit a form, click a link, and get some information. What's the easiest way to do this (keeping the session open, etc.).
Javascript would be a better solution than PHP. You can use it in tandem with PHP to submit a form that references the same page, ie. <form method='index.php' action='post'>
If method is GET then you ought to be able to work it out form the URLs of a few real world attempts.
It POST then you are probably SOL unless it's your own web page./app and you know what $_POST it expects ... unless you find a tool to snoop your HTTP traffic and get the POST info from observing a few real wrold examples.
You can use CURL in PHP to simulate submitting data, clicked links, etc., I suppose, but a client-side scripting language like Javascript--as opposed to a server-side language like PHP--is more suited to what you're describing. I'd need more info to give you a specific example.
You will not be able directly emulate those events in PHP as web apps use Javascript on the client side and PHP is a different language and operates on the server side.
Firstly, I would see if there is an open API available for the web app you're wondering about, e.g. Gmail: http://code.google.com/apis/gmail/ . Not all APIs can do what the web app can do, so you'll need to check the documentation to make sure the API does what you want and has an easy way to interface with PHP.
The other option is to essentially reverse engineer how the web app communicates with it's server. Most all web apps operate by sending POST or GET HTTP data in some sort of serialized format like XML, JSON or text. You can use something like the Firebug add-on for Firefox to view POST/GET data. If you know what the server sends to the client and what the client sends to the server, you can essentially write a script using something like CURL to emulate the client in PHP instead of JavaScript. This would take quite a bit of work and probably involves a lot of trail & error.

Possible to use Javascript to get data from other sites?

Is it possible for a web page using Javascript to get data from another website? In my case I want to get it for calculations and graphing a chart. But I'm not sure if this is possible or not due to security concerns. If it is considered a no no but there is a work around I would appreciate being told the work around. I don't want to have to gather this information on the server side if possible.
Any and all help is appreciated.
Learn about JSONP format and cross-site requests (http://en.wikipedia.org/wiki/JSON#JSONP).
You may need to use the "PHP-proxy" script at your server side which will get the information from the websites and provide it to yours Javascript.
The only reliable way is to let "your" webserver act as a proxy. In PHP you can use curl() to fire a HTTP request to an external site and then just echo the response.
You can't pull data from another server due to the same origin policy. You can do some tricks to get around it, such as putting the URL in a <script> tag, but in your case it wouldn't work for just parsing HTML.
Use simple_dom_html, to parse your data server side. it is much easier than doing it in JavaScript anyways.
A simple way you might be able to do this is to use an inline iframe. If the web page you are getting the data from has no headers, or you can isolate the data being pulled in (to say an image or SWF), this might work.
cross-domain javascript used to be impossible, using a (php-)proxy was a workaround for that.
jsonp changes this entirely, it allows to request javascript from another server (if it has an API that supports jsonp, a lot of the bigger webplayers like google, twitter, yahoo, ... do), specifying the callback-function in your code that needs to be triggered to act on the response.
the response in javascript will contain:
a call to a callback-function you defined
the actual payload as a javascript-object.
frameworks like jquery offer easy support for jsonp out of the box.
once you have the raw data you could tie into google chart tools to create graphs on the fly and insert them in your webapp.
Also worth considering is support for XMLHttpRequest Access Control which is support in some modern browsers.
If the service provider that you are trying to access via a web page has this set up, it is a very simple call to XMLHttpRequest and you will get access to the resources on that site without the need for JSONP (especially useful for requests that are not GET, i.e. POST, HEAD etc)

Categories