3

I've been trying to pull Survey Data for a client from their Survey Monkey account, it seems that the more data their is the more likely illegal characters are introduced in to the resulting JSON string.

Below is a sample of what is returned on a bad response, every response is different and even shorter requests some times fail leaving me at a miss.

{
  "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 can't fathom as to why this is happening, the more parameters in the fields option there are, the more illegal characters are introduced. It isn't just illegal invalid characters, some times random letters are thrown in as well which prevents me from handling the data correctly.

I am using Laravel 4 with the third party Survey Monkey library by 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 can't be resolved I'll have to resort to asking the client to manually import CSV files which isn't ideal and introduces possible user error.

On a side note, I don't see this issue cropping up when using the same parameters on the Survey Monkey console.

O/S: Windows 8.1 with WAMP Server

Code used to execute the request

$Surveys = SurveyMonkey::getSurveyList(array
(
    'page_size' => 1000,
    'fields' => array
    (
            'title', 'question_count', 'num_responses', 'date_created', 'date_modified'
    )        
));

The SurveyMonkey facade is a custom package used to integrate the original Survey 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 request returns the same troublesome characters, nothing else was used to get the response.

Using the following code

echo "\n".mb_detect_encoding($result, 'UTF-8', true);

This code shows the charset for the response, when successful and no illegal characters are present (there are still random characters in the wrong places) it returns that it is in fact UTF-8, when illegal characters are present false is returned so nothing is outputted. More often than not, false is returned.

Everon
  • 3,789
  • 1
  • 12
  • 12
  • What OS are you doing this on? Am I correct in assuming the issue is the `�`? – Steven Aug 03 '14 at 19:36
  • 1
    Can you add the relevant code you're using to make the request? – mjk Aug 03 '14 at 19:37
  • Can you try replicating the request with `curl` (the command line utility) and see if the problem is still there ? If it is then it's most likely a problem on their side. –  Aug 03 '14 at 19:57
  • Updated the question, I'll attempt a raw CURL request now. – Everon Aug 03 '14 at 20:24
  • Did you try testing the hardware? – Ignacio Vazquez-Abrams Aug 03 '14 at 20:33
  • Not sure what you mean by testing the hardware, the `curl` version has been added to the question with the same results in the response unfortunately. – Everon Aug 03 '14 at 20:55
  • Try on another computer. If the problem persists, the problem is on their end. If the problem goes away, then the problem is with your computer/server. – Laurence Aug 03 '14 at 23:32
  • I'll attempt to run the same `curl` code on another machine (linux VPS) and post the results. – Everon Aug 04 '14 at 00:52
  • I can confirm that whilst running on another machine that the correct data is being returned in the response in a proper form. Finally, what would cause a `curl` request to return strange characters? – Everon Aug 04 '14 at 00:59
  • Downloaded AMPPS to see if the issue was still present with a different server stack. Behold, the issue has disappeared with the new stack leaving me completely dumb founded but happy that I can now continue developing. Still, I have no idea as to why these random characters were introduced. If any one has a good idea and posts it as an answer I will mark it. Thank you all for the help finding a way around this issue. – Everon Aug 04 '14 at 02:56

1 Answers1

0

Maybe I'm grossly oversimplifying the whole thing and apologies if so, but I have had these funny little chars pop in to results, too. They were leading and trailing whitespace. Can you trim data on retrieve and see if it still happens?

Sundance.101
  • 404
  • 3
  • 10
  • See the comments on the main question, a solution of sorts was found. Trimming the response would do nothing as the characters are not at the beginning and ends of the array and occur more than once, they're also random and very often invalid characters. The solution was to use a different server stack after it was suggested to check the hardware, the nearest thing to a migration available was moving from WAMP Server to AMPPS. – Everon Aug 18 '14 at 18:34
  • If a solution was found, please write it as an answer and then accept it. – philshem Apr 22 '15 at 20:59