7
Contributors
11
Replies
61
Views
4 Years
Discussion Span
Last Post by diafol
Featured Replies
  • I'm sure it is not possible. It is their hosted platform and they don't allow you to use any server side code with it. Read More

  • 3
    diafol 3,720   4 Years Ago

    OK, got it to work. :) **NOTE** - To anybody reading this, I will be removing the 'remote php file' from my site in a few days, but I include the code, so you can try it out yourself on your own remote servers. ## BLOGGER CODE ## Just before … Read More

  • Nice work diafol, that is a cool workaround. Read More

1

I'm sure it is not possible. It is their hosted platform and they don't allow you to use any server side code with it.

0

how implement php in my blogspot

Can you be more specific what you want php in blogger?

What php code feature you are trying to implement?

0

As pixelsoul states the blogger platform is hosted, so you're out of luck. You may be able to get some functionality from javascript though. You can link to jQuery and use JSONP to retrieve json data, which can then be formatted within js and displayed. Haven't tried it, so don't know if it would work.

It does mean that your external php page returns any data in json format. This could be used to poll a database, etc.

0

A other less elegant option may be to insert an iframe since blogger allows you to modify the template. The source of the iframe could be from an externally hosted site that is running your PHP code.

3

OK, got it to work. :)

NOTE - To anybody reading this, I will be removing the 'remote php file' from my site in a few days, but I include the code, so you can try it out yourself on your own remote servers.

BLOGGER CODE

Just before the </head> tag in the blogger html:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Then just before the </body> tag in the blogger html:

<script>
//auto run - or place inside a function if you need it to run say off a button click
  var request = $.ajax({
    type: "GET",
    url: "http://dw.diafol.org/scripts/blogger.php?request=menu",
    dataType: "jsonp"
    });

    request.done(function(msg) {
        alert(msg.menu);
    });
</script>

You can try the code above on the url listed - it should give you some output to alert. jQuery adds a callback=... parameter automatically to ensure jsonp. If you try to get direct data from this url from your address bar, you can see the difference between jsonp and json:

http://dw.diafol.org/scripts/blogger.php?request=menu&callback=boo  (JSONP)
http://dw.diafol.org/scripts/blogger.php?request=menu (JSON)

REMOTE PHP FILE

Anyway,

Here's the php script in blogger.php

<?php
$return = false;

if(isset($_GET['request']) && $_GET['request'] == 'menu')
{
    $array = array("menu"=>"<ul><li>Home</li><li>Page1</li><li>About</li></ul>", "alert"=>"This worked!!"); 
    $return = true;
}

if(isset($_GET['request']) && $_GET['request'] == 'fruity')
{
    $array = array("fruit"=>array("Bananas","Apples","Oranges"));
    $return = true; 
}

if($return)
{
    $r = json_encode($array);
    echo (isset($_GET['callback']) && trim($_GET['callback'])!='') ? $_GET['callback'] . "($r)" : $r;
}

?>

So pretty simple. This page will accept requests for 'fruity' and 'menu'.

The js script can pick up the data in the 'done' function for menu as msg.menu and msg.alert
and for fruity as msg.fruit

Obviously, this doesn't do much, the php code could be written to do something useful such as retrieve data from DB etc. This could be developed further to create an API on your remote site, utilising REST.

In addition, here's the script in action on my temporary (testing) blog: http://trialjs.blogspot.co.uk/ OR go here: http://jsfiddle.net/xgX66/

Edited by diafol

Votes + Comments
This is very neat! =)
Beautiful solution
0

Just a further thought - you could include data from sites not set up for jsonp by scraping them with file_get_contents() or cURL and then processing the data to return. Of course, you'd have to lock it down securely to avoid running malicious scripts.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.