how i can implement php code in my blogspot website name text free. how is is it possible? i want add little programme in my blogspot site

Recommended Answers

All 11 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.

Member Avatar for LastMitch

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?

Member Avatar for diafol

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.

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.

Member Avatar for diafol

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/

commented: Beautiful solution +7
commented: This is very neat! =) +12

Nice work diafol, that is a cool workaround.

Member Avatar for diafol

Thanks :)

Member Avatar for diafol

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.

You better put php in your blog if you have a database.GETS??!!!

I think it is not possible to implement php in my blogspot.

Member Avatar for diafol

I think it is not possible to implement php in my blogspot.

Why? Can't you use ajax/jsonp?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.