Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@starbuck93
Created December 31, 2015 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starbuck93/06c139f92158908c0270 to your computer and use it in GitHub Desktop.
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"}
<?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;
?>
@GregoryConrad
Copy link

That is pretty funny; since you didn't get the email!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment