Facebook Cookbook - Jay Goldman [150]
Solution
Creating and modifying listings both use the incorrectly named Marketplace.createListing() method. If you’re creating a new listing, pass a 0 for the lid (listing ID):
$attributes = array('title'=>'Bees!','category'=>'FORSALE',
'subcategory'=>'GENERAL','description'=>'Great big hive full
of bees for sale. Makes great honey.');
$listing = $facebook->api_client->marketplace_createListing(0, true, $attributes);
If you’re modifying an existing listing, pass the listing’s lid:
$attributes = array('title'=>'Free Bees!','category'=>'FORSALE',
'subcategory'=>'GENERAL','description'=>'I\'m covered in beeeeeees!
Please take them. Please.');
$listing = $facebook->api_client->marketplace_createListing
(23464075249, true, $attributes);
Discussion
This method (along with Users.setStatus()) requires that users grant your application an extended permission. If you call it without having the permission granted, you’ll get back a 280 error (“Creating and modifying listings requires the extended permission create_listing”). See Extended Permissions for more information on extended permissions.
The attributes array can contain settings for any of the properties of a Marketplace listing, which you can find in Listing Table.
The middle parameter is a boolean indicating whether the entry should show up on the user’s Profile. Passing true will publish a News story about the user, like the one shown in Figure 9-14.
Figure 9-14. News Feed of new listing
There’s one more parameter you can pass in: a uid to indicate who should own the new listing. This is ignored for desktop applications, and it will be honored for web apps only if that user has granted the extended permission. If you’re not using the Client Library, this parameter is required if you don’t pass in a session_key.
Get Marketplace Listings
Problem
I need to retrieve Marketplace listings, based on either a list of users or listing IDs.
Solution
Use the Marketplace.getListings() method:
$listings = $facebook->api_client->marketplace_getListings('1234567', null);
where 1234567 is the lid (listing ID).
If you specify no uid (user ID) as the second parameter, you’ll get the listings you’ve asked for with no filtering. You can specify a uid without lids to retrieve all the listings owned by one user:
$groups = $facebook->api_client->marketplace_getListings(null, 12345);
Both parameters can be specified as arrays as well:
$targetListings = array('1234567', '8901234');
$targetUsers = array('12345', '67890', '11223');
$listings = $facebook->api_client->marketplace_getListings
($targetListings, $targetUsers);
Discussion
Marketplace.getListings() returns a multidimensional array of listing records, with each element containing the fields listed in Table 8-13.
FQL equivalent
If you’d prefer to use FQL to access listings, the equivalent query is:
SELECT listing_id, url, title, description FROM listings WHERE poster in
($uids) AND listing_id in ($listing_ids)
See Table 8-13 for more information.
Get Marketplace Categories and Subcategories
Problem
I need to pull in the list of Marketplace categories and subcategories.
Solution
Use the Marketplace.getCategories() method:
$categories = $facebook->api_client->marketplace_getCategories();
Once you have a category, use the Marketplace.getSubCategories() method to retrieve its subcategories:
$subcategories = $facebook->api_client->marketplace_getSubCategories('FORSALE');
Discussion
You can quite easily combine the two methods to generate a