The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Making Slim output JSONP

Craig Turner's Avatar

Craig Turner

07 Jun, 2013 09:26 PM

Hi there,

i've been playing around trying to get Slim to output JSONP instead of JSON. Having read a few forum post's i found a small function to insert the callback var around the encoded json response.

For some reason which i can't explain, the callback comes before the json response and doesn't wrap around it instead.

Does anyone have any ideas why this is please?

Thanks in advance :D

A part of my code:

require 'Slim/Slim.php';

$app = new Slim();


function jsonpWrap($jsonp) {
  $app = Slim::getInstance();

  if (($jsonCallback = $app->request()->get('callback')) !== null) {
    $jsonp = sprintf("%s(%s);", $jsonCallback, $jsonp);
    $app->response()->header('Content-type', 'application/javascript');
  }
  return $jsonp;
}


// Schedule API
$app->get('/schedule', 'getSchedule');
$app->response()->body(jsonpWrap(json_encode($app)));

$app->run();


// Begin Function


// Begin Functions for Schedule

function getSchedule() {
    $sql = "select * FROM schedule";
    try {
        $db = getConnection();
        $stmt = $db->query($sql);  
        $users = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);

    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    }
}
  1. 1 Posted by juanes on 08 Jun, 2013 07:41 AM

    juanes's Avatar

    Hi Craig,

    In your code you're setting the body content before the $app->run() so the contents returned by the jsonpWrap function are set before the route.

    You might want to use the jsonpWrap function in the route callable instead. You're also doing a json_encode() on the $app object, which is probably wrong since that's the Slim's object, you might want to pass the json_encode($users) instead.

  2. 2 Posted by Craig Turner on 08 Jun, 2013 08:44 AM

    Craig Turner's Avatar

    Hi Juanes,

    Thanks for replying to me. Being new to Slim i'm not quite sure how i can use the jsonWrap functions in the route callable instead?

    Also, to pass the json_encode($users) instead would i just change the line:

    $app->response()->body(jsonpWrap(json_encode($app)));
    

    to:

    $app->response()->body(jsonpWrap(getSchedule()));
    

    Sorry for sounding dumb, i'm just trying to get my head around getting this to work.

  3. 3 Posted by viks on 05 Apr, 2016 05:18 AM

    viks's Avatar

    good Once we done download the Mobdro Freemium variant, we can join to advanced https://mobdrodownloads.com That’s it! Nowadays we happen to be ready to streams video recording on our Android os units employing Mobdro No cost App ! nice.

  4. 4 Posted by meet on 25 Apr, 2016 06:50 AM

    meet's Avatar

    good any additional system customer and vice a versa. It fundamentally ensures http://shareitforpcz.com This iphone app involves various different features which I have always been referencing nice.

  5. 5 Posted by jamessandoeval on 06 Jun, 2016 06:33 AM

    jamessandoeval's Avatar

    good The earliest period you manage the addon installer http://kodiaddonstv.com/phoenix-kodi-addon-install/ but the virtually all remarkable and effective great.

  6. 6 Posted by William Peloqui... on 09 Jun, 2016 05:53 AM

    William Peloquin's Avatar

    It's good to know that videoder is now updated with lots of new appealing features like multiple downloading , direct audio downloading and many more. So get your app here @ https://video-der.com/

  7. 7 Posted by prabhas on 10 Jun, 2016 04:26 AM

    prabhas's Avatar
  8. 8 Posted by james wells on 22 Jun, 2016 09:52 AM

    james wells's Avatar

    You tv Player download free here https://youtvplayer.org/ !! Get the APK file, You TV player for PC, Android, iPhone! Best LIVE TV STREAMING APP available online.

  9. 9 Posted by c4598738 on 22 Jun, 2016 01:34 PM

    c4598738's Avatar
  10. 10 Posted by John on 04 Jul, 2016 01:23 PM

    John's Avatar

    whatsapp app is very popular to use on your computer and laptop http://appsforpcway.com/whatsapp-for-pc-download-windows-7-computer-free/ just download and install it on your Windows or MAC device and start using it. nice.

  11. 11 Posted by John on 11 Aug, 2016 08:04 PM

    John's Avatar
  12. 12 Posted by 1commheartno on 27 Aug, 2016 12:04 PM

    1commheartno's Avatar

    I guess JSON is much easier than JSONP. HeartBurn No More reviews

  13. 13 Posted by 1commbloodpre on 02 Sep, 2016 06:57 AM

    1commbloodpre's Avatar

    There are many programs that are now coded in JSON. click here

  14. 14 Posted by pufixoya on 24 Sep, 2016 12:05 PM

    pufixoya's Avatar

    Now microsoft making thing easy for user.
    http://youngbodyrebootsystem.com/

  15. 15 Posted by dowuxire on 17 Oct, 2016 02:42 AM

    dowuxire's Avatar

    Can you please tell me where I can learn more about JSONP. Mike Madigan's Numerologist

  16. 16 Posted by tloombc1st on 21 Oct, 2016 03:22 AM

    tloombc1st's Avatar

    JSONP is great tool. I use it many time. Read Here

  17. 17 Posted by thestepbc1st on 22 Oct, 2016 02:21 AM

    thestepbc1st's Avatar

    Can you tell me which is the best place to learn JSON? Read this article

  18. 18 Posted by ken on 26 Nov, 2016 07:25 AM

    ken's Avatar

    good it is employed to operate all Google android applications on laptop computers. https://ishowboxdownloadapp.com End user can choose video resolution according to your gadget compatibility best.

  19. 19 Posted by kyzu on 28 Nov, 2016 09:12 AM

    kyzu's Avatar

    I really love using JSONP as it is nice and easy to use.
    How to Get Her Back Now

  20. 20 Posted by browncarrie2012 on 19 Dec, 2016 10:53 AM

    browncarrie2012's Avatar

    good It's compatible with a very wide range of data layouts https://kodidownloadtv.com/kodi-for-iphone-ipad-ios-download-app without jailbreak.Vshare app allows users to download best.

  21. 21 Posted by Brandon on 18 Feb, 2017 11:54 AM

    Brandon 's Avatar

    good film in hd all free of charge. You could additionally stream http://vidmatedownloadz.com/vidmate-for-pc-download-app-windows-1087xp-mac-ios your data and also download Vidmate apk for android nice.

  22. 22 Posted by jjshtt on 09 Mar, 2017 11:23 AM

    jjshtt's Avatar

    good It pressurizes the water to travel through a thin https://watergadget.com which we receive from our visitors. If you have best,

  23. 23 Posted by ricky on 22 Apr, 2017 04:42 AM

    ricky's Avatar

    This application after some updates obtain a cozy and also https://imoappz.com ways to download IMO for PC. We will clarify you plainly to download IMO for PC.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac