View Full Version : API Search Integration
Hello,
I am hoping someone can point me in the right direction here please?
I have got to grips with the "ps_client.php" file and now would like to format the results of a search. At the moment the output is given by "print_r($response);", this obviously just prints the whole result as an object.
I have tried to put the result into an array, which gives an error message as objects cannot be formatted into arrays.
Could someone please point me in the direction to format the results using php?
Thanks in advance.
Simon
You'll need to brush up on some PHP object orientated programming syntax as most of the API is object orientated. If you're familiar with outputting arrays it's almost the same concept, just different :D
To loop through an object:
foreach($oResponse as $obj) {
echo $obj->sName;
}
It depends exactly what $oResponse is (use the print_r output to understand the structure of what you're dealing with) but basically you access the object properties with $foo = $object->$property.
Hope that helps. Post the print_r output if you still have problems and I'll give you a more specific example.
Hi Steve,
Thanks for your help with this. You are right I do need to brush up on my soap skills. Anyway here is the output.
stdClass Object
(
[searchProductReturn] => Array
(
[0] => stdClass Object
(
[iId] => 2356704
[iMerchantId] => 48
[sMerchantProductId] => 0415351693
[iUpc] => 0
[iEan] => 0
[iIsbn] => 415351693
[sName] => POPULAR AND THE CANONICAL
[sDesc] => POPULAR AND THE CANONICAL by
[sSpec] =>
[sPromo] => This volume ranges from the Second World War to the postmodern, considering issues of the ''popular'' and the competing criteria by which literature has been judged in the later twentieth century.
[sBrand] => TAYLOR & FRANCIS LTD
[sModel] =>
[iCategoryId] => 538
[sAwDeepLink] => http://www.awin1.com/pclick.php?p=2356704&a=61612&m=48&platform=cs
[sAwThumbUrl] => http://images.productserve.com/thumb/48/2356704.jpg
[sAwImageUrl] => http://images.productserve.com/preview/48/2356704.jpg
[sCurrency] => GBP
[fSearchPrice] => 16.9
[fRrpPrice] => 18.99
[sDisplayPrice] => GBP16.90
[sMd5] =>
)
[1] => stdClass Object
(
[iId] => 2357914
[iMerchantId] => 48
[sMerchantProductId] => 0415262003
[iUpc] => 0
[iEan] => 0
[iIsbn] => 415262003
[sName] => Twentieth-Century Caribbean Literature
[sDesc] => Twentieth-Century Caribbean Literature by Alison Donnell
[sSpec] => New edition
[sPromo] => Traces the processes by which a 'history' and canon of Caribbean literature and criticism have been constructed. Focusing on Anglophone writings from across the twentieth century, this title asks what it is that we read when we approach this Literature, how it is that we read it and what pressures may have influenced our choices and approaches.
[sBrand] => Taylor & Francis Ltd
[sModel] =>
[iCategoryId] => 538
[sAwDeepLink] => http://www.awin1.com/pclick.php?p=2357914&a=61612&m=48&platform=cs
[sAwThumbUrl] => http://images.productserve.com/thumb/48/2357914.jpg
[sAwImageUrl] => http://images.productserve.com/preview/48/2357914.jpg
[sCurrency] => GBP
[fSearchPrice] => 15.11
[fRrpPrice] => 17.99
[sDisplayPrice] => GBP15.11
[sMd5] =>
)
)
[iLimit] => 9
[iOffset] => 360
[iReturnCount] => 9
[iTotalCount] => 1000
[aKeywordSuggestions] => Array
(
)
)
Thanks,
Simon
OK, so your result set, $oResponse is an object that contains an element called "searchProductReturn" which is an array of product objects.
So... if you want to print a list of product titles you would do the following:
#loop through each array element
foreach($oResponse->searchProductReturn as $product) {
#print the product name
echo $product->sName . '<br />';
}
This basically loops through each product object in the array, accessing whatever properties you need, in this case the product name which is stored in "sName". Try some of the others and you'll get the idea.
This principle works for most of the API - make a call, print_r the result to see what you're getting back and then recode it to loop through the results printing stuff out.
Let me know how you get on.
Edit: Just remember this:
use $object->property to get values from an object
and $array[key] to get values from an array.
There's a complicated mixture of objects and arrays but just pick your way through it by using print_r to check what you're doing.
Hi Steve,
Thanks for your direction. That has got me on my way!!
Would you be able to recommend a good book for me to sharpen my soap skills on?
Regards,
Simon
Good to hear. There's not much more to the API than that.
I'm self taught with PHP so I'm afraid I can't recommend any books. I basically just picked apart other people's code and used PHP.net and Google for the stuff I couldn't figure out.
Thankfully with the SW API all of the real SOAP stuff is abstracted - you never really need to go near SOAP unless you want to write your own classes. Which is great, because I know chuff all about SOAP! Just find a good PHP 5 book that focuses on OOP.
IntroSites
03-07-08, 23:21
I'm self taught with PHP so I'm afraid I can't recommend any books. I basically just picked apart other people's code and used PHP.net and Google for the stuff I couldn't figure out
Snap, the best way to learn PHP etc is to start as a "copy n paste coder" :D