How to pass PHP functions to JS use

Source: Internet
Author: User
Tags soap
Project in back-end PHP has some string handler functions, or string legality detection class
Web front end also need to use, do not want to write JS code alone, maintenance is very troublesome, there will be small differences may lead to new problems
My idea is to write a uniform interface on both sides of JS and PHP, define a set of rules for function calls, how functions are passed, and so on, and return the results by JSON.
With these questions:
1. Heard the word webservice, it refers to the design idea, or the actual PHP module it?
2. How can security and privacy be ensured by this method of calling functions over the network? I just want to be called by my own project.
3. Is there a network delay in this approach, and is there a better solution?
Can 4.nodejs really implement the front-end common code? Can I put an example?


Reply to discussion (solution)

1. The definition of WebService is actually very wide, in fact, it is a kind of remote call technology.
2. Authentication-based, back-end and previous-segment interactions in the same domain can be based on the user authentication of the Web site itself, and the backend-to-backend interaction verification is implemented in much the same way.
3. Simple string processing and other functions (not based on the complex business of relying on data query) should also be implemented in JS, network problems can not be solved.
4. Web-based is not possible.

In fact, the landlord wants to achieve something, the control of the ASP.

Write a unified interface on both sides of JS and PHP, define a set of rules for function calls, how functions are passed, and so on, and return results by JSON

Use?? Yes, but what about the generation? is to be divided?? JS and PHP?

Normal is the way to do it!
The front end passes the sensitive data, the service side processing completes returns the JSON data!
But the ordinary string processing suggestions do not put the service side, increase the service pressure! Try to handle it on the client!

PHP functions can not be used in JS, the reason is simple: the two grammatical rules are different.
So, as long as the syntax rules are the same, you can call each other, like Nodejs and JavaScript.
Of course, because both are at both ends of the network, there is the problem of communication. Fortunately, Nodejs is inherently supportive of the Websock protocol, and communication problems are internally hidden.
But this is obviously not a problem with the PHP layout discussion.

Microsoft has been pushing the client's B/s, so there's a client-side control in vs. (just write code on the server)
It actually implements the communication between the browser and the server via the XMLHTTP component.
And this technique is the buzz of the AJAX technology that was later fired

WebService is the Web service
The strange thing is why you have to separate it. is the ordinary website not also a Web service?
If http://localhost/add.php?a=1&b=2 returns 3, it is called a Web page
So http://localhost/index.php?m=add&a=1&b=2 also returns 3, why is it called a remote call?
The Get, post method, which uses HTTP only, is called a Web page
While using the HTTP other such as put, delete .... method is called REST
In fact, there is no need to tangle in some nouns, as long as the goal can be achieved.

PHP currently offers a
The primary stage of XML-PRC WebService
Advanced stage of SOAP WebService

But because of the popularity of JSON, these things are not popular.
Just because. Net and Java have not yet been able to provide the official JSON operation function, can not exit the historical stage

1. The definition of WebService is actually very wide, in fact, it is a kind of remote call technology.
2. Authentication-based, back-end and previous-segment interactions in the same domain can be based on the user authentication of the Web site itself, and the backend-to-backend interaction verification is implemented in much the same way.
3. Simple string processing and other functions (not based on the complex business of relying on data query) should also be implemented in JS, network problems can not be solved.
4. Web-based is not possible.

In fact, the landlord wants to achieve something, the control of the ASP.



2. Hope to get more specific technical implementation details to prevent off-site calls
3. So in a way to solve ~ do not want to realize the reasons for JS as the main building said, I personally do not like to write the same function 2 code, too awkward
4. The biggest obstacle to solving this problem is that PHP and JS syntax do not work, but nodejs not, so, the latter is most likely to solve the
No contact with ASP. NET, do not know how to achieve the extent of it?

Write a unified interface on both sides of JS and PHP, define a set of rules for function calls, how functions are passed, and so on, and return results by JSON

Use?? Yes, but what about the generation? is to be divided?? JS and PHP?



Logically I think it can be shared: Pass in a string, determine whether it is a mailbox, return a Boolean, this Boolean value, regardless of the front and back are available

Normal is the way to do it!
The front end passes the sensitive data, the service side processing completes returns the JSON data!
But the ordinary string processing suggestions do not put the service side, increase the service pressure! Try to handle it on the client!



Well, the server pressure is really a wake up
So you want to find a better way, without taking up the additional request, the PHP function will be automatically ported to JS

So many projects, this kind of simple verification of code really not much, and client-side validation also has a dedicated library, the code will not write a lot, duplicate code is not much.
Like to confirm that the user name does not exist in this way, the client itself can not be implemented, there is no more consideration.

So you want to find a better way, without taking up the additional request, the PHP function will be automatically ported to JS



Unless you can develop a compiler

PHP functions can not be used in JS, the reason is simple: the two grammatical rules are different.
So, as long as the syntax rules are the same, you can call each other, like Nodejs and JavaScript.
Of course, because both are at both ends of the network, there is the problem of communication. Fortunately, Nodejs is inherently supportive of the Websock protocol, and communication problems are internally hidden.
But this is obviously not a problem with the PHP layout discussion.

Microsoft has been pushing the client's B/s, so there's a client-side control in vs. (just write code on the server)
It actually implements the communication between the browser and the server via the XMLHTTP component.
And this technique is the buzz of the AJAX technology that was later fired

WebService is the Web service
The strange thing is why you have to separate it. is the ordinary website not also a Web service?
If http://localhost/add.php?a=1&b=2 returns 3, it is called a Web page
So http://localhost/index.php?m=add&a=1&b=2 also returns 3, why is it called a remote call?
The Get, post method, which uses HTTP only, is called a Web page
While using the HTTP other such as put, delete .... method is called REST
In fact, there is no need to tangle in some nouns, as long as the goal can be achieved.

PHP currently offers a
The primary stage of XML-PRC WebService
Advanced stage of SOAP WebService

But because of the popularity of JSON, these things are not popular.
Just because. Net and Java have not yet been able to provide the official JSON operation function, can not exit the historical stage



Thank you Xu Big, long a lot of knowledge

Very interested why Nodejs fire, what is the cause of the sudden rise?
See a lot of people say the biggest reason is JavaScript, so that front-end engineers take no pains to turn back
I think since they are family, want to achieve the point string processing, should not need network communication, directly to the machine
For example, in the HTML request a function.js file, is actually nodejs the original back-end code directly copied in the
Although not this version of the content also want to know

But when it comes to third-party interfaces, it's been a couple of times that the word soap
Because the SDK exists, but also to determine the approximate principle, did not go to check, the original is that a set of normative nature of things ~
Before the iOS app colleague asked me, you backstage is WebService?
I said no, just like a normal web page, output JSON string only

The main selling point of Nodejs is non-blocking I/O
To say that. NET Java does not have the operation JSON thing, in fact PHP also only Json_encode, Json_decode just.

Different Php js method
Therefore, to???? The words.

If it is submitted to the service contractor each time, the service will be a big force.

So many projects, this kind of simple verification of code really not much, and client-side validation also has a dedicated library, the code will not write a lot, duplicate code is not much.
Like to confirm that the user name does not exist in this way, the client itself can not be implemented, there is no more consideration.



You're right, but I'm thinking more possibilities: Web development still has a lot of room to upgrade.

There is a possibility, but it should not be limited to code replication ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.