xml 将Yotpo Stars(每个产品和网站评论)保存到自定义字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml 将Yotpo Stars(每个产品和网站评论)保存到自定义字段相关的知识,希望对你有一定的参考价值。

<?php
// This was done under 1hr, change the curls if needed.
$url = 'http://staticw2.yotpo.com/batch';
$app_key = 'YOURAPIKEY';
//$pid = $_GET['Product_Code']; // Testing
$pid = $_POST['Product_Code'];

function execute_post($url, $data){
	//encode the data as json string
	$requestBody = json_encode($data);

	//initialise curl session
	$ch = curl_init();

	//curl options
	curl_setopt($ch, CURLAUTH_BASIC, CURLAUTH_DIGEST);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . 'application/json' ,'Content-Type: application/json'));

	//curl execute and json decode the response
	$responseBody = json_decode(curl_exec($ch));

	//close curl session
	curl_close($ch);
	return $responseBody;
}

function http_request($url) {
    $ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_POST => false,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1,
        CURLINFO_HEADER_OUT    => true,
        CURLOPT_HTTPHEADER => array(
            //'Content-type: text/html',
           // 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36',
        )
    ));
    $response = curl_exec($ch);
    $err     = curl_errno( $ch );
    if($err){
        
        die($err);
    }
    curl_close($ch); 
     
    return $response;
}

if ($pid) {

	$data = array(
		'methods' => array(
				array(
					'method' => 'bottomline',
					'params' => array(
						'pid' => $pid,
						),
					'format' => 'json'
					),
			),
		'app_key' => $app_key
		);

	$stars = execute_post($url, $data);
	//print_r($stars);

	$totalReviews = $stars[0]->result->total;
	$average = $stars[0]->result->average_score;

	$postCustomFieldsURL = 'http://www.YOURDOMAIN.com/mm5/merchant.mvc?Screen=PROD&Store_Code=YOURSTORECODE&updateproductCode='. $pid . '&yotpoReviewRating='. $average . '&yotpoReviewsCount=' . $totalReviews;
	http_request($postCustomFieldsURL);

	echo 'Product Total: '. $totalReviews . ' / Average: '. $average;
	echo "\n";
  // For some reason when i added the app_key as a variable, it didn't work. MAke sure to change this
	$siteReviewURL = 'https://api.yotpo.com/v1/widget/YOURAPPKEY/products/yotpo_site_reviews/reviews.json?per_page=1&page=1';
	$siteReviews = http_request($siteReviewURL);
	$siteReviews = json_decode($siteReviews);

	$siteTotal = $siteReviews->response->bottomline->total_review;
	$siteAverage = $siteReviews->response->bottomline->average_score;
  // Created an inactive product that stores all the site reviews: SITE-REVIEW || you don't have to do this but it helps.
	$postCustomFieldsURL = 'http://www.YOURDOMAIN.com/mm5/merchant.mvc?Screen=PROD&Store_Code=YOURSTORECODE&updateproductCode=SITE-REVIEW&yotpoReviewRating='. $siteAverage . '&yotpoReviewsCount=' . $siteTotal;
	http_request($postCustomFieldsURL);

	echo 'Site Total: '. $siteTotal .' / Average: '. $siteAverage;
	
}
?>
// -- Save Yotpo Reviews to Custom Field -- //
$(window).load(function(){
	var productCode = $('#product-code').text();

	function saveRating(){
		// ajax call to page that will save the rating and count
		$.ajax({
			url: '/mm5/forms/yotpoStars.php',
			type: 'POST',
			cache: false,
			data: {
				Product_Code: productCode
			},
		   success: function(data){
				//console.log(data);
		  }
		});
	}
	saveRating();
});
<mvt:if expr="g.updateproductCode">
	<mvt:item name="customfields" param="Write_Product_Code( g.updateproductCode, 'yotpoReviewRating', g.yotpoReviewRating )" />
	<mvt:item name="customfields" param="Write_Product_Code( g.updateproductCode, 'yotpoReviewsCount', g.yotpoReviewsCount )" />
<mvt:exit />
</mvt:if>

以上是关于xml 将Yotpo Stars(每个产品和网站评论)保存到自定义字段的主要内容,如果未能解决你的问题,请参考以下文章

html Yotpo API POST

将 XML 文件同步到 MySQL 数据库

HDU - 5126 stars (CDQ分治)

15英寸四核酷睿i7本 Acer 5750G抢先评

使用亚马逊产品广告 API - Python 搜索书籍

POJ 2352 Stars 线段树