-
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PETA Vegan Approved List to OBF #131
Comments
PETA Vegan Approved does not support food/cosmetics, etc. but only clothing, so it's not relevant. <?php
ini_set("display_errors", 1);
ini_set("display_startup_errors", 1);
error_reporting(E_ALL);
$VAR = $_GET['company'];
if (empty($VAR)){
header("Content-Type: application/json;charset=utf-8");
echo '{
"STATUS": "BAD_REQUEST"
}';
exit();
}
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://petaapprovedvegan.peta.org/search-peta-approved-vegan/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"action\":\"facetwp_refresh\",\"data\":{\"facets\":{\"search\":\"$VAR\",\"categories\":[],\"country\":[],\"pagination\":[]},\"frozen_facets\":{\"search\":\"soft\"},\"http_params\":{\"get\":{\"_search\":\"$VAR\"},\"uri\":\"search-peta-approved-vegan\",\"url_vars\":[]},\"template\":\"wp\",\"extras\":{\"sort\":\"default\"},\"soft_refresh\":0,\"is_bfcache\":1,\"first_load\":0,\"paged\":1}}",
CURLOPT_HTTPHEADER => [
"Accept: */*",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$peta = json_decode($response)->template;
$petali = strip_tags($peta, "<h2><div>");
$first_step = explode( '<div class="company-filter__companies">' , $petali );
$second_step = explode("</div>" , $first_step[1] );
$h2 = $second_step[0];
$dom = new \DOMDocument();
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $h2);
$array = [];
foreach ($dom->getElementsByTagName("h2") as $node) {
if ($node->textContent !== "No Companies Match Your Search"){
$array["STATUS"] = "COMPANIES_FOUND";
$array["PETA_VEGAN_APPROVED"][] = $node->textContent;
}
else {
$array["STATUS"] = "NO_COMPANIES_FOUND";
}
}
header("Content-Type: application/json;charset=utf-8");
echo json_encode($array, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)."\n";
}
?> |
But I did not stop there, PETA has also a list of Cruelty-Free Cosmetics and personal care products: I contacted them and while they support the idea, they don't give out a JSON list or anything like that (apparently it is, because it's updated too often? I didn't really get it.) <?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://tierversuchsfrei.peta-approved.de/wp-admin/admin-ajax.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
/* Security is the AJAX-Security Code generated by PETA and might need to be changed, for faster testing replace "all" with "50"*/
CURLOPT_POSTFIELDS => "security=74eceaf085&action=dynamic_filtering&sort=companies-dont-test&per_page=all",
CURLOPT_HTTPHEADER => [
"Accept: */*",
"Content-Type: application/x-www-form-urlencoded",
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$ul = json_decode($response)->html;
$petali = strip_tags($ul, "<li>");
$peta = preg_replace("/\n|\t/", "", $petali);
$dom = new \DOMDocument();
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $peta);
$array = [];
foreach ($dom->getElementsByTagName("li") as $node) {
$array[] = $node->textContent;
}
$output = [];
array_walk($array, function ($entry) use (&$output) {
/* PETA Business Friend & Features PETA Logo */
if ("FL" == substr($entry, -2)) {
if(!empty(substr($entry, 0, -2))){
$output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -2);
}
}
/* Features PETA Logo */
elseif ("L" == substr($entry, -1)) {
if(!empty(substr($entry, 0, -1))){
$output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -1);
}
}
/* PETA Business Friend */
elseif ("F" == substr($entry, -1)) {
if(!empty(substr($entry, 0, -1))){
$output["PETA_DOES_NOT_TEST"][] = substr($entry, 0, -1);
}
}
/* Does not test on animals */
else {
$output["PETA_DOES_NOT_TEST"][] = $entry;
}
});
header("Content-Type: application/json;charset=utf-8");
echo json_encode($output, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)."\n";
}
?> |
A first little test is online: |
EAN Code to test with: |
Proposed changes
Add the PETA Vegan Approved list to all products from OBF and match them.
The text was updated successfully, but these errors were encountered: