Bad JSON is returned from Survey Monkey (get_survey_list)
I was trying to pull Survey data for a client from my Survey Monkey account, it seems that the more data there is, the more likely illegal characters are injected into the resulting JSON string.
Below is an example of what is returned with a bad response, each response is different and even shorter queries keep me from missing multiple times.
{
"survey_id": "REDACTED",
"title": "REDACTED",
"date_modified": "2014-XX-18 17:59:00",
"num_responses": 0,
"date_created": " 2014-01-21 10:29:00",
"question_count": 102
}
I cannot understand why this is happening, the more parameters in the field option, the more illegal characters are introduced. These are not just illegal invalid characters, sometimes random letters are also generated which prevent me from processing the data correctly.
I am using Laravel 4 with the third Monvey Monkey group from oori https://github.com/oori/php-surveymonkey
Any help would be appreciated in tracking down the issue, the deadline is pretty tight and if this cannot be resolved I will have to resort to asking the client to manually import the CSV files, which is not ideal and introduces possible custom error.
On the other hand, I don't see this issue occurring when using the same parameters in the Survey Monkey console.
O / S: Windows 8.1 with WAMP server
Code used to make the request
$Surveys = SurveyMonkey::getSurveyList(array
(
'page_size' => 1000,
'fields' => array
(
'title', 'question_count', 'num_responses', 'date_created', 'date_modified'
)
));
The facade SurveyMonkey
is a special package used to integrate the original Monvey Monkey library located here:
https://github.com/oori/php-surveymonkey/blob/master/SurveyMonkey.class.php
Raw PHP cURL request
$header = array('Content-Type: application/json','Authorization: Bearer REDACTED');
$post = json_encode(array(
'fields' => array(
'title', 'question_count', 'num_responses', 'date_created', 'date_modified'
)
));
$post = json_encode($post);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=REDACTED");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
The above query returns the same annoying characters and nothing was used to get the answer.
Using the following code
echo "\n".mb_detect_encoding($result, 'UTF-8', true);
This code shows the encoding of the response, when successful and no illegal characters are encountered (there are still random characters in the wrong places), it returns that it is actually UTF-8 when illegal characters are encountered. false so nothing is output. Most often, false is returned.
source to share