Created
December 31, 2015 00:48
-
-
Save starbuck93/06c139f92158908c0270 to your computer and use it in GitHub Desktop.
Gets the Raspberry Pi Zero stock from Adafruit and returns json data to use at endpoints like this: {"stock":"0"}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://www.adafruit.com/products/2885"); //https://www.adafruit.com/products/2817 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
$return = curl_exec($ch); | |
curl_close($ch); | |
$find = '<meta name="twitter:data2" content="'; | |
$find2 = '">'; | |
$outOfStock = "OUT OF STOCK"; | |
$inStock = "64 IN STOCK"; | |
$inStock2 = "IN STOCK"; | |
$data = substr($return, strlen($find)+strpos($return, $find, strlen($find)),strlen($outOfStock)); | |
if ($data != $outOfStock) { | |
//string substr ( string $string , int $start [, int $length ] ) | |
//mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) | |
$data = substr($return, strlen($find)+strpos($return, $find),strpos($return, $inStock2,(strlen($find)+strpos($return, $find)))-strpos($return, $find)-strlen($find)); | |
// substr($return, start at the end of $find, go to the beginning of $inStock2) | |
} | |
else $data = "0"; | |
// echo $data . "\n"; | |
$json_string = '{"stock":"' . $data . '"}' . "\n"; | |
echo $json_string; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That is pretty funny; since you didn't get the email!