question

clickimusprime avatar image
0 Likes"
clickimusprime asked ·

PHP Proxy example

This is an example of a PHP proxy script that uses a &amp;amp;quot;mycall&amp;amp;quot; parameter to allow you to use one script for multiple requests. You could have a command to getebayitemsbykeywords and another switch case to get item by category, etc. Or you can just put the contents of the switch case in a separate proxy script and use a different PHP script for each call. Data is sent from jQuery (or whatever) to the PHP page in JSON format, the proxy fetches data in JSON format from eBay, and passes it back to the client in JSON format. Instead of sending all the data, you should preprocess it in PHP, extract the data you need (title, img, price) and send only that data back to the client. Untested code, you may have to tweak it a bit. <pre> <?php // myproxy.php $mycall = $_REQUEST["mycall"]; switch($mycall) { case "getebayitemsbykeywords": // get whatever parameters you sent from javascript // put the changing variables in javascript and the fixed variables in php // e.g. you don't need to put your appid in javascript and pass it, // you can just have it on the php side // in fact, for fixed queries, you don't need to pass any parameters at all, you // can just hard code them all on the php side $keywords = rawurlencode($_REQUEST["keywords"]); // replace with your ebay api call $url = " https://svcs.ebay.com/services/search.... &RESPONSE-DATA-FORMAT=JSON &keywords=$keywords"; // better to use cURL here, do retries, etc. $response = file_get_contents($url); // extract data here and return only what's needed // encode the extracted data in json // or just send it all: echo $response; // ebay response is in JSON already exit; break; } ?> // jQuery var json = {}; json.mycall = "getebayitemsbykeywords"; json.keywords = "iphone 7"; $.ajax({ dataType: "json", method: "POST", data: json, url: "/ myproxy.php", }).done(function( data ) { console.log(data); }).fail(function(jqXHR, textStatus ) { errmsg = "Request failed: " + textStatus; console.log(errmsg); }); </pre>
errorfinding apicallbackjqueryproxy
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

clickimusprime avatar image
0 Likes"
clickimusprime answered ·
The example is for jQuery. The data is returned as an object named data in the done function. You can rewrite it to mimic the eBay setup if you like, and it may save some additional work, but that wasn't the intent of the example. Again, it's better to parse the data server side and only send what you need to the client, especially if you are paying for outbound data usage. You can also avoid the javascript processing and just include the eBay items in the page markup. There are advantages to both approaches.
· Share
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

vintage-adventures avatar image
0 Likes"
vintage-adventures answered ·
The new problem we have, since end of September, with eBay's Finding API no longer honoring the 'callback' parameter in the request URL (from javascript), is the return payload is not wrapped in the function signature that javascript uses to parse and display the eBay items on our websites. The javascript function is defined and loaded in client's browser when visitors hit our sites. It is there, waiting for the payload to be returned from eBay's API, but the payload must be wrapped, so the function will get invoked and do its work. Yes, your proxying code could be modified to solve our problem, but when the payload is returned to JS from PHP, it needs to be wrapped in the function. The defined and listening function in JS is: _cb_findItemsByKeywords( . . . // definition for parsing and displaying returned payload ) So, on your line 20, I propose the following. // better to use cURL here, do retries, etc. $response = file_get_contents($url); $response = &quot;_cb_findItemsByKeywords($response)&quot;; OR // better to use cURL here, do retries, etc. $response = file_get_contents($url); $response = '_cb_findItemsByKeywords(' . $response . ')'; Jim @woog91, what do you think? related thread: https://forums.developer.ebay.com/questions/32460/finding-api-returns-302-moved-temporarily.html?childToView=33630#answer-33630
· Share
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.