zend - upload file with REST services - php

i use zend framework and work with some web services(insert, update, delete but with int, string parameters). Now i don't how to upload file(for example image) with zend and web services(REST). Does anyone have some example?

You should send the file throught post call. The Rest service will receive the postAction as when you do what ever other functions. But, with files you cannot make the calls with ajax, so you have a little trick to do in client side.
Remember, REST service will implement post action and receive the parameters as you would receive normally. If you know how to receive and manipulate a file with standard php, you can do it in REST service implementation.
The trick on client side is to create an invisible iFrame and redirect the submit form target to it.
This links explain this method very well with code samples:
form POST in iframe without affecting history
http://ajaxpatterns.org/IFrame_Call
http://geekswithblogs.net/rashid/archive/2007/08/01/Create-An-Ajax-Style-File-Upload.aspx
http://www.openjs.com/articles/ajax/ajax_file_upload/
http://viralpatel.net/blogs/2008/11/ajax-style-file-uploading-using-hidden-iframe.html
NamastĂȘ !!

Related

How to accept post variables send from other domain php?

one of my vendor he is asking a posting page where he can send some varaibles from his portal which is in asp.net.
My website is in php. what posting page i should be providing him and how to fetch whatever he is sending.
You may disable the CSRF validation (if you have any) to achieve this task.
However the standard way is to create webservice to achieve this goal.
The best and easiest API to implement is RESTful.
Here is reference to that:
How to create a simple REST API in PHP
Creating a simple REST API in PHP
It doesn't matter what language the vendor use or what you use...
He need to you to specify him what is your script URL that you allow him and expect POST request from him to do what he need.
If it's from his server side (for example in PHP we use curl),
so he need to use asp.net way to post request...
maybe this will help him how to do it
How to make HTTP POST web request
If he doing those request from client side (ajax),
so he can use jQuery http://api.jquery.com/jquery.ajax/
and in your side to allow in the php script the "access control allow origin"
see:
how to bypass Access-Control-Allow-Origin?

How to Implement Multiple REST APIs in HTML?

HTML page has a submit button. On clicking the button, the 4 APIs has to be called. All the 4 APIs are linked to each other.
For instance, the first API is used to get the access token. And the token is passed in the second API to process the GET request.
I am new to REST APIs and not sure whether it's easy to call via HTML or better way by using PHP.
You want to use, not to implement API.
You should decide first on which side (server or client) it should be implemented.
On server side you do requests on your behalf. You are responsible for them. They are done from your IP address. But you can hide API address from client, you can cache response or control this process somehow. Use PHP cURL extensions for that.
On client side, requests is performed by end user, from his IP. Read How to enable cross-domain request on the server? to learn about requests to different domains. Search for AJAX. Probably, use some library like jQuery to write less code.

D2L Valence - post feedback for a dropbox with no submission

I am currently implementing an app using the D2L Valence API. The goal is to upload 1 file per student (different for each student though) in some course. The file is now uploaded as a feedback for a combination of course-dropbox-student. Everything works perfectly as long as there is a submission from that student. If there isn't any - the call fails. I know this is possible to do via the UI (i.e. if I try to post a feedback through the UI for a user with no submission, it still works), but my question is would it be possible to do the same via API call. I tried simply attaching the file using this call (the file is uploaded before doing that) or posting a feedback and then attaching the file to it. Nothing works unless there is a submission. Thanks for any help.
I believe you're correct that the API doesn't currently support this behaviour (submitting feedback against a dropbox with no submission). If this behaviour does exist in the WebUI, then this would run contrary to D2L's stated goal that the API should be at feature/behaviour parity with the Web UI. I would therefore encourage you to report this as a defect via your organization's ASC or partner or account manager.

How to handle PUT and DELETE HTTP request in a PHP API First App?

I am wanting to build an API first RESTful application in PHP. I have never attempted to do this so I have some questions about how to handle PUT and DELETE
So for an example if I have a API endpoint that updates a User profile, should I make it accept BOTH a POST and PUT Request?
If I was building a Client for my API as a Desktop app or iOS app, etc it would be easy to send a PUT request to my API but I plan to have a Web based app for my API as well.
So on my web based app, I would have an HTML Form to Update a User profile, this would then be sent as a POST as HTML Forms do not allow PUT requests.
Could someone with more experience with this explain the best way to handle my example scenario?
Would the proper way be to send my Form as a POST to my PHP script, then my PHP script would make a proper PUT request to my PHP API with cURL?
You can absolutely also do PUT requests from browsers, but you need javascript.
Generally I would say a good way to think about it, is as follows:
First build a great REST api that follows all the rules. Only once you are at that point, think about the workarounds you need to make it work in other contexts. Submitting an HTML form is a valid thing to need a workaround for.
However, since 'POST' is completely open for interpretation, and has little rules associated, one option would be to create a single resource (or url) on your server that handles all the POST requests coming from browsers. (something like /browserpost).
You could always add a hidden <input> field with name="url" that specifies which resource was actually intended to be updated, and an <input> with name="method" value="PUT" for the intention.
You will need to add CSRF protection anyway, so I feel this would be a solid way to deal with this this. 1 endpoint to specifically 'proxy' html-based form submissions and internally do the appropriate mappings to the correct REST services.
I would use GET POST PUT DELETE as they are described in HTTP. That's restful (in my opinion). As regular browser forms does not support this I would send the data via AJAX.
If you really need to use browser forms, maybe because javascript is not enabled, then using POST requests with a param like ?method sounds like a suitable solution - although I don't like it.

How can i implement linkedin inviter to my website using PHP?

i read the documentation of linkedin inviter from the link http://developer.linkedin.com/docs/DOC-1012 and there is no other help on internet inspite of this page. but what i found is only the XML things on this page. i dont know how to use it and what should be required basically to make the code work. please post or give reference to a complete code for linkedin invitation in PHP. i would greatly be obliged .
There is a LinkedIn-recommended library for PHP that has a specific invite() method that you could use:
http://developer.linkedin.com/docs/DOC-1255
Direct link to code:
http://code.google.com/p/simple-linkedinphp/
Essentially, you pass it the method of invitation (by email, member id), pass it the recipient, subject, message, and it creates the XML and sends it via OAuth to LinkedIn.
There's also a demo script included, and a live demo showing how to use the invite() method here:
http://simplelinkedin.fiftymission.net/
OK I took a brief look at that API you are talking about. The API requires that you first build a XML file and then use the "POST" method to send it to the provided URL. I should mention that I have never attempted this before, so I may be incorrect, but the process seems pretty straightforward, if somewhat confusing according to the LinkedIn API docs.
So you have several options:
I believe that you can use AJAX to post the XML to the LinkedIn API. In order to do this, you would set up an AJAX call that uses the "POST" method. Here is a link on how to construct a AJAX POST request (its a little different than a "get" request). So essentially you would take your users input out of a form, pack it into a series of structured XML nodes (just like if you were building a XML file) and then take that dynamically generated block of XML and insert it into the body of the AJAX request. Send the request and then parse the results.
Alternatively you could set up a blank XML "template" file with the structure you need to submit the invitation to the LinkedIn API. When your user submits the invitation request to your server you would extract their input, validate it, and then use a combo of PHP Xpath and PHP DOM to search through your XML template and dynamically add the user data to the template at the correct node locations. Then use these instructions on this page to send your invitation request to the API handler at LinkedIn (uses PHP function "curl()").
Let me know how it turns out!
Regards,
H

Categories