Last active
June 8, 2022 09:55
-
-
Save omrico1/548c94e20727bed11438 to your computer and use it in GitHub Desktop.
Yotpo Batch Request and Parsing Example
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 | |
//This is an example batch request for app_key GYRCLYSnfHKGlA9iQoYjeVQ0297Fr4kvW5kaZ9Zw, please remember to switch to your own. | |
$url = 'http://staticw2.yotpo.com/batch'; | |
//Building the batch data | |
$data = array('methods' => ' | |
[{"method":"main_widget","params":{"pid":"295823037"}}, | |
{"method":"bottomline","params":{"pid":"295823037", | |
"link":"", | |
"skip_average_score":false}} | |
]', | |
'app_key' => 'GYRCLYSnfHKGlA9iQoYjeVQ0297Fr4kvW5kaZ9Zw'); | |
// Creating the request headers, important to use application/x-www-form-urlencoded as Content-type, | |
// Also remember that the request method is HTTP POST, and that the request can be done on HTTPS and on HTTP. | |
$options = array( | |
'http' => array( | |
'header' => "Content-type: application/x-www-form-urlencoded\r\n", | |
'method' => 'POST', | |
'content' => http_build_query($data), | |
), | |
); | |
$context = stream_context_create($options); | |
$result = file_get_contents($url, false, $context); | |
// Parsing the response | |
$response = json_decode($result, true); | |
$main_widget = $response[0]["result"]; | |
$bottom_line = $response[1]["result"]; | |
// Now echo either one of $main_widget or $bottom_line variables, should yield the HTML of the relevant widget. | |
?> | |
<!-- We will now build a demo product page, with static HTML content pulled from the batch request above. --> | |
<html> | |
<head> | |
<script type="text/javascript"> | |
(function e(){ | |
var e=document.createElement("script"); | |
e.type="text/javascript", | |
e.async=true, | |
e.src="//staticw2.yotpo.com/GYRCLYSnfHKGlA9iQoYjeVQ0297Fr4kvW5kaZ9Zw/widget.js"; | |
var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)})(); | |
</script> | |
</head> | |
<body> | |
<h1> My Product </h1> | |
<div | |
class="yotpo bottomLine" | |
data-appkey="GYRCLYSnfHKGlA9iQoYjeVQ0297Fr4kvW5kaZ9Zw" | |
data-product-id="295823037" | |
data-name="Everything But Sticks" | |
data-url="" | |
data-image-url="//cdn.shopify.com/s/files/1/0040/6842/products/EverythingBut1_large.png%3Fv=1382385048" | |
data-description="...."> | |
<!-- this entire inner html will be overriden by the widget.js JS lib --> | |
<?php echo $bottom_line ?> | |
</div> | |
<h2> Reviews: </h2> | |
<div | |
style="width:700px" | |
class="yotpo yotpo-main-widget" | |
data-appkey="GYRCLYSnfHKGlA9iQoYjeVQ0297Fr4kvW5kaZ9Zw" | |
data-product-id="295823037" | |
data-name="Everything But Sticks" | |
data-url="" | |
data-image-url="//cdn.shopify.com/s/files/1/0040/6842/products/EverythingBut1_large.png%3Fv=1382385048" | |
data-description="...."> | |
<!-- this entire inner html will be overriden by the widget.js JS lib --> | |
<?php echo $main_widget ?> | |
</div> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment