I use the Amazon product advertising API to retrieve product information on my websit in a JSON array.
When calling the API, an example result I am getting is the following, which is shown when using print_r($response).
{"ItemsResult":{"Items":[{"ASIN":"B010FTYIUS","DetailPageURL":"https://www.amazon.com/dp/B010FTYIUS?tag=hangarflights-20&linkCode=ogi&th=1&psc=1","Offers":{"Listings":[{"Id":"DAjnW3%2BLO70EsyLN%2FMQLwyvR8afWckVN8fvJ6mojtJQMdPh0yCEgxtv3gBjPMOwUQp8RvEP56ao%2FWFAtjhHkFRQRsYs27pFtiLuL9DiaqdIprrqGBw03IQ%3D%3D","Price":{"Amount":1095.95,"Currency":"USD","DisplayAmount":"$1,095.95"},"ViolatesMAP":false}]}}]}}
How can I get, for example, just the price amount (here 1095.95)? I have tried multiple things such as echo $response["ItemsResult"]["items"]["Offers"]["Listings"]["Price"]["Amount"] but without success.
$response = '{"ItemsResult":{"Items":[{"ASIN":"B010FTYIUS","DetailPageURL":"https://www.amazon.com/dp/B010FTYIUS?tag=hangarflights-20&linkCode=ogi&th=1&psc=1","Offers":{"Listings":[{"Id":"DAjnW3%2BLO70EsyLN%2FMQLwyvR8afWckVN8fvJ6mojtJQMdPh0yCEgxtv3gBjPMOwUQp8RvEP56ao%2FWFAtjhHkFRQRsYs27pFtiLuL9DiaqdIprrqGBw03IQ%3D%3D","Price":{"Amount":1095.95,"Currency":"USD","DisplayAmount":"$1,095.95"},"ViolatesMAP":false}]}}]}}';
$response = json_decode($response, true);
echo $response["ItemsResult"]["Items"][0]['Offers']["Listings"][0]["Price"]["Amount"]; //1095.95
Related
I'm trying to get list of invoices for specific customer from Stripe using PHP.
Following is the code I'm using.
\Stripe\Stripe::setApiKey('STRIPE_SECRET_KEY');
$response = \Stripe\Invoice::all(array('customer' => 'CUSTOMER_TOKEN'));
But this returns empty array.
Instead, CURL command returns result
curl https://api.stripe.com/v1/invoices?customer=CUSTOMER_TOKEN -u STRIPE_SECRET_KEY
Any idea?
I'm using Stripe PHP library available from Github.
https://github.com/stripe/stripe-php
$response = \Stripe\Invoice::all(array('customer' => 'cus_id'));
You need to add customer id for specific customer.
What I'm trying to do is make a twitch follower alert in PHP. The only stuff so far that I have gotten done is find out where to get the info from. I need assistance decoding the info, and then getting the user name and setting it to a string. The place for the recent followers is: https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1
The data Twitch's API gives you is in JSON format (JavaScript Object Notation). You can decode it using json_decode($data, true). This gives you an associative array with the required fields. For example, to get the name of the user who most recently followed:
json_decode($twitch_data, true)['follows'][0]['user']['name']
Update:
Here's a more detailed answer. First, you're going to have to get the data from the Twitch API via a get request using file_get_contents(). Then, you parse the resulting JSON using the method described above and echo that out onto the page. Here's the full code:
<?php
$api_url = 'https://api.twitch.tv/kraken/channels/trippednw/follows/?limit=1';
$api_data = file_get_contents($api_url);
$last_follow = json_decode($api_data, true)['follows'][0]['user']['name'];
echo "The last user to follow <strong>trippednw</strong> on Twitch is <strong>" . $last_follow . "</strong>.";
?>
I'm trying to list all the manufacturers names and ID in Magento through SOAP but I couldn't find a sample of code to do it. Can anyone help on how to achieve this using SOAP and PHP?
You should be able to do this using the Product Attributes API. Documentation link http://www.magentocommerce.com/api/soap/catalog/catalogProductAttribute/product_attribute.info.html
The below code should get you an attribute and the values associated with it. You simply need to pass in the attribute code. Then pull out the options value from the response object which should contain an array of catalogAttributeOptionEntity which will be your options and values.
$client = new SoapClient('http://magentohost/api/soap/?wsdl');
// If somestuff requires api authentification,
// then get a session token
$session = $client->login('apiUser', 'apiKey');
$result = $client->call($session, 'product_attribute.info', 'manufacturer');
var_dump ($result);
// If you don't need the session anymore
//$client->endSession($session);
I would advise you to take a llok to this article that shows hos to easily consume the Magento SOAP Web service https://www.wsdltophp.com/Blog/Use-WsdlToPhp-to-manage-your-Magento-website-with-its-SOAP-API
So I'm trying to get details of a specific venue using PHP. Here's my code that attempts to use a GET request to the Foursquare API to return results and then process them as JSON and display the name, address and city:
$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "https://api.foursquare.com/v2/venues/4b522afaf964a5200b6d27e3");
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curlhandle);
curl_close($curlhandle);
$json = json_decode($response);
foreach ($json->groups[0]->venues as $result)
{
echo $result->name.' - '.$result->address.' '.$result->city."<p />";
}
What am I doing wrong? I'm completely new to PHP and the Foursquare API so it could be something glaringly obvious.
You don't need to authenticate using the OAuth flow to get venue information, but you do need to add your Client ID and Client Secret to the API call.
So, the URL should be something like:
"https://api.foursquare.com/v2/venues/4b522afaf964a5200b6d27e3?client_id=CLIENT_ID&client_secret=CLIENT_SECRET
In JavaScript, the URL should be
`https://api.foursquare.com/v2/venues/${venue_id}?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&v=20180323`
Note, I am using template literals and don't forget v=20180323 because the Foursquare API no longer supports requests that do not pass in a version parameter. Of course, you can modify the version number to keep updated.
You need to authenticate your request, if you go to the URL you get this.
{"meta":{"code":400,"errorType":"invalid_auth","errorDetail":"Missing access credentials. See https:\/\/developer.foursquare.com\/docs\/oauth.html for details."},"response":{}}
So I'd say you need to authenticate by following this: https://developer.foursquare.com/overview/auth.html
This worked for me: (on Python)
url = 'https://api.foursquare.com/v2/venues/{0}'.format(self.placeid)
params = dict(
client_id=self.clientid,
client_secret=self.clientsecret,
v='20170801'
)
r = requests.get(url=url, params=params)
I'm trying to get nearby venues. I found this url where i can just plug in lat and lon.
How do i use this URL? I tried getting the libraries from Foursquare API site, but can't get them to work.
I know i can json_decode the result on this page to get a php object, but how do i make a call and get the data to my webpage?
https://api.foursquare.com/v1/venues.json?geolat=40.562362&geolong=-111.938689&l=10
Thanks
$data = file_get_contents(http://MyURL/);
$results = json_decode($data, true); //true = making it into array, not object