Skip to content

Commit

Permalink
improve json parsing - use Yii's JsonHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
engram-design committed Apr 11, 2016
1 parent 81d45f3 commit 6dc2d0c
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions feedme/services/FeedMe_FeedJSONService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,17 @@ public function getFeed($url, $primaryElement) {
return false;
}

// Perform cleanup on raw data first
$raw_content = preg_replace("/[\r\n]+/", " ", $raw_content);
$json = stripslashes($raw_content);
$json = utf8_encode($json);
$json = StringHelper::convertToUTF8($json);

// Parse the JSON string
$json_array = json_decode($json, true);
// Parse the JSON string - using Yii's built-in cleanup
$json_array = JsonHelper::decode($raw_content, true);

// Look for and return only the items for primary element
$json_array = craft()->feedMe_feed->findPrimaryElement($primaryElement, $json_array);

if (!is_array($json_array)) {
craft()->userSession->setError(Craft::t('Invalid JSON.'));
FeedMePlugin::log('Invalid JSON.', LogLevel::Error, true);
$error = 'Invalid JSON. - ' . json_last_error_msg();

craft()->userSession->setError(Craft::t($error));
FeedMePlugin::log($error, LogLevel::Error, true);

return false;
}
Expand Down

0 comments on commit 6dc2d0c

Please sign in to comment.