PDA

View Full Version : The official PHP integration thread!


alexboyer
03-29-06, 04:40 PM
Ok, since this hasn't been created yet, I think this thread should be dedicated to tips and tricks for MM5 users who wish to integrate Miva Merchant into an existing PHP site. PHP is quite powerful and I've been able to accomplish a great number of things using a combination of PHP, Miva and the MySQL database.

Any users wishing to supply tips and tricks they may find valuable to other users needing similar things can post them here.

Similarly, if anyone has questions regarding these techniques, ask away.

Just as a footnote, it has been mentioned, and I happen to agree, that using multiple languages on one site isn't ideal, only that it's possible.

With that said, hopefully I'm not the only one using PHP/Miva hehe. :D

truXoft
03-29-06, 04:52 PM
Hmm, why is this thread named "The Official PHP integration"? Did Miva announce any such plans, or was it initiated by any Miva employee?

Personally, I discourage people from using two different engines for parsing one page unless it is absolutely unavoidable. Starting the parser or virtual machine costs CPU time, memory and other resources, and using two of them in the same time only aggravates the situation. I'd rather suggest learning Miva Script - it is a very easy to learn scripting language, yet quite powerful. Of course, there are situations where you cannot avoid the heterogeneous scripting language environment, but you should try to avoid abusing it. Especially try avoiding mixed scripts when you have a website with heavy traffic or are hosted on a shared server.

alexboyer
03-29-06, 05:02 PM
Just as a footnote, it has been mentioned, and I happen to agree, that using multiple languages on one site isn't ideal, only that it's possible.
Your concerns were already addressed in my initial post. The title was only slightly tongue-in-cheek. It's not an official "Miva" thing, just a bit of humor since nobody has started a thread regarding the two yet.

Oh and just FYI, even though I agree with you about learning Miva script, it would be unreasonable to tell someone that every feature on their existing site should be ported to Miva script, which is the other reason for starting this thread.

Our site is 16GB containing hundreds of pages and several user features all developed with PHP and we burn through almost 2 Terabytes of bandwidth each month. With the kind of traffic we get, I really wasn't prepared to approach management and say "Well I can't work on the site because I need to learn this new language and do a complete site conversion."

I wanted to integrate Miva throughout our existing site in ways that would appear seamless to the customer. That process is still ongoing, but through much trial and error I have learned how to work with them both. It would be quite difficult to convert every feature on our site to Miva script just to keep the number of running languages to one.

So once again, the point of this thread is primarily for users who already have an existing site written in PHP and who either don't have the time or inclination to learn a new language.

Hope that clears it up.:)

truXoft
03-29-06, 05:15 PM
Your concerns were already addressed in my initial post.Yes, sorry, I've overseen it. Nevertheless it is never enough warning people of it. I see far too often attempts for integrating Miva applications with PHP (or other) scripts absolutely without any reason, and often the solution is done in Miva Script much easier and faster than in PHP.

alexboyer
03-29-06, 05:18 PM
Yes, sorry, I've overseen it. Nevertheless it is never enough warning people of it. I see far too often attempts for integrating Miva applications with PHP (or other) scripts absolutely without any reason, and often the solution is done in Miva Script much easier and faster than in PHP.
Agreed. Users just starting out should learn Miva and do it all with a single language. They'll save themselves endless time and headaches that way.

jason - jmh web services
03-29-06, 05:51 PM
just fyi. sebenza studios (http://www.sebenzastudios.com) has been integrating and doing jedi stuff with php and miva for years.

tipiyano
07-12-06, 02:38 AM
We are building a new online store. However, unlike other stores, our store sells products as well as services. In services, customers can buy certain type of tests and view the results online in a few days. Also, we sell custom designed images that users can dynamically create on the website (using their test result). Among products, we have frames and merchandised that can be personalised with the images. Now here's the problem:

The algorithms for image processing are written in ASP.NET by the contractor.
We have a miva hosting and would like to use it to set up shop.
We have backend that has been setup with mysql where test results are stored
There are other miscellaneous website features (display custom image IN the frame chosen, pull test results from mysql, mathematical calculations) all of which can be easily implemented in php.

Unfortunately, the image code will stay in ASP. We figured that we can host that software and query it remotely over the network (which again php can do). So to me it seems like that I have to use PHP (which I know) and avoid learning MIVAscript (which I am not sure can handle all the requirements).

Please let me know how would I go about integrating the php and miva or if there's another simpler way to acheive the same results. I am experienced with php but just learning MIVA.

Thanks a lot for your help,
Rahul
biosketch.net

alexboyer
07-12-06, 03:04 AM
What specifically do you want to accomplish with PHP?

Scott McCollough
07-13-06, 12:09 AM
The merger of PHP and MivaScript is sometimes a necessary evil. Case in point, this forum.

vBulletin is written in PHP, yet we may want to add in MivaScript widgets. Oddly enough, the PHP directive "include" seems to work fine when you point it to an external web site. For example:


<?php
include( 'http://www.miva.com/some_script.mvc' );
?>


Live and learn,

/Scott

rich1000
07-19-06, 08:03 AM
Ok, so can someone tell me HOW I can learn miva script then? The do***entation is scattered and hard to find. I am simply trying to find ways to do simple things I can easily do in PHP.

I agree that if miva can do these simple things, I'd like to learn. But such simple things as learning how to query the database proves to be difficult to learn.

I like the mySQL backend, but I can't seem to find good do***entation on how I can query it thru miva.

Here's two examples of things I'm trying to do, that I could whip out in PHP in a second. Finding info on how to do it in miva is another story.

Example 1)
I'd like to query the Categories table for each cats whose parent_id = 12
From that I'd like to construct links basically like this (for each)

a href="/mm5/merchant.mvc?Screen=CTGY&Store_Code=TCA&Category_Code={category ID}> {category NAME} /a


Example 2)
I'd like to make breadcrumbs (yes I know about modules, but don't answer this by telling me about some module)

On a category screen:
Query to pull in the current category code, and construct the breadcrumbs based on the heirarchy of parent id categories.


I'd love it if there was easily found information on doing typical programming queries, conditionals, etc. on the mySQL tables in miva. Right now, I'm finding some things that explain briefly some built in functions in miva.

thanks for any help on getting started

Vic - WolfPaw Computers
07-19-06, 03:38 PM
All of the docs are pretty much in one place:

http://smallbusiness.miva.com/support/docs/api/

You also need to understand some fundamental differences.

MIVA is the company. Merchant is the software. MIVAScript is the programming language.

It takes some time and studying to learn how to code in MIVAScript. Look at examples provided in the docs, forums, etc. Download the Merchant 4 API kit. Perhaps even the Limited Source Kit.

While the reference material is not the best, it is out there and not as scattered as it used to be.

alexboyer
07-19-06, 04:48 PM
The merger of PHP and MivaScript is sometimes a necessary evil. Case in point, this forum.

vBulletin is written in PHP, yet we may want to add in MivaScript widgets. Oddly enough, the PHP directive "include" seems to work fine when you point it to an external web site. For example:


<?php
include( 'http://www.miva.com/some_script.mvc' );
?>


Live and learn,

/Scott

Yep, since you're including a non PHP file, it gets parsed by the Miva Engine first and passed to the PHP page as plain HTML.

rich1000
07-19-06, 05:46 PM
All of the docs are pretty much in one place:

http://smallbusiness.miva.com/support/docs/api/

You also need to understand some fundamental differences.

MIVA is the company. Merchant is the software. MIVAScript is the programming language.

It takes some time and studying to learn how to code in MIVAScript. Look at examples provided in the docs, forums, etc. Download the Merchant 4 API kit. Perhaps even the Limited Source Kit.

While the reference material is not the best, it is out there and not as scattered as it used to be.

Yup... I understand the fundamental differences between the 3 names. I'm also working in Miva 5. Is the structure and API support of Merchant 4 the same as 5?

I guess I mispoke. I understand I can create mivascript to do the things I want to do. But I assumed with the new miva 5 the tokens (entities and items) would have most of what I need to do, and some of the simpler things. Am I right in thinking that it's still very limited what I can do within Merchant with regards to this new method in 5?

I'll look at MivaScript more and see how well it digests. But like the previous post: being able to include MVC within a PHP file... I just wonder why it can't the other way around. Same thing with an SSI to a CGI. I could create small "widgets" that pull things from the database, much easier than I can learning how to create mivascript and then determining how that integrates with Merchant.

Vic - WolfPaw Computers
07-19-06, 05:52 PM
Merchant 5's API is very similar to 4's. A few functions were eliminated, others were added. That is covered in the Merchant 5 API docs and database schema.

You cannot do it the other way around because the Empresa Engine will not parse PHP. That is a limitation of the software.

I think the do***entation for the API for 5 is still very lacking, yes. Many questions have been answered in the developers forum however.

Scott McCollough
07-25-06, 10:00 PM
I haven't tested this, but you should be able to use MvCALL to an external .php script and then parse the results. So you CALL out to a .php file that queries the databases and send back an XML response with the cat_id and cat_name.

rich1000
07-25-06, 10:06 PM
I did find a module: External File Pro by ADS that seems to do what I was needing to do. I haven't worked enough to do my programming tasks and see if it works for all aspects, but so far it looks like it does what it says.

wetlander
07-26-06, 03:13 AM
how exactly would one access the mysql data in miva from php? and i cannot afford a modual for anything, so please dont suggest that.

dotCOM_host
07-26-06, 03:53 AM
Well, start learning - this isn't a 2 minute job unless you know PHP and mySQL pretty well. Here's a good tutorial you can start with:

http://www.freewebmasterhelp.com/tutorials/phpmysql/

wetlander
07-26-06, 03:14 PM
I know both pretty well, in miva 4 I did most of my work utilizing open UI for the miva inner stuff, and php 'work arounds' for most everything else. I just can't seem to wrap my brain around how to get it to connect right. I will learn the miva script one way or another, but their are things i need php for which miva either would not be able to do, or php can handle more efficently.

dotCOM_host
07-26-06, 08:16 PM
Like this:

<?php
$username = "mySQL_username";
$password = "mySQL_password";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
print "Connected to MySQL<br>";
// do your stuff here - whatever you need to do
mysql_close($dbh);
?>

Brandon MUS
08-29-06, 06:52 PM
We are switching to Miva 5, and I no longer use Miva's login Screens or the PROD Screen either. These three screens are now all written in PHP because of the extra requirements we had. Our Miva 4 was customized heavily by a third party company a long time ago and also contained extra modules that are not available for Miva 5 yet. So instead of trying my hand at writing MivaScript to make up the functionality that we had, we decided that PHP would give us the ability to expand functionality, faster, cheaper, and easier.

percy
07-11-07, 03:25 AM
Hi there,

I am one of those who don't want to learn Mivascript because i don't have the time to learn a new language. I already have some php scripts that i want to integrate into Miva 5 and I have read through this thread and many other online documents but still can't do this. can someone please help me out. any help will be appreciated.

thanks

alexboyer
07-13-07, 06:37 PM
What specifically are you trying to do?

percy
07-13-07, 09:00 PM
Hi,

In the Customer Account, I want to add 2 fields. One asks the customer if they want to participate in a Rewards program we are offering (they can say YES or NO). The other field asks them for the the e-mail of the person who referred them to our company.

When a customer checkout their order, I collect the information I need, namely, Customer name, e-mail, password, address, orders made (and the cost), etc. This is the kind of information I pull from Miva Merchant in order to update the Database and perform the necessary calculations. For example:

If customer B says they want to participate in the rewards program, and they were referred by customer A then i have to link B to A in a list. Then if B makes a purchase, I keep track of orders made. THen I perform the calculations that are necessary so that i can assign commissions to A who referred B to us.

I have already written all the code in PHP and designed a prototype for the system. the prototype works fine, but now i need to integrate it into my miva store. THis morning I actually bought a module called ADS External File Pro Module, to help me make use of my php scripts. I just installed it, but idon't know how it works yet....thats what i'm working on right now.

any suggestions or advice will be greatly appreciated!

Thanks

johnny_come_lately
09-24-07, 04:48 AM
Can PHP be used in Miva 4, too? Can it be used any where, a template, a category page, a product page? Can a call to the db (that miva normally accesses) be done with PHP and the results displayed? Basically, I have a miva site (not PHP and integrate miva 5 into it as per OP) and want to know if I can use PHP in it.

Thanks. :confused:

Scott McCollough
09-26-07, 09:37 PM
PHP *should* work in MivaScript 4.x just as easily (read as: lots of trial and error) as in 5.x.

Since you're running MivaMerchant 4.x, it would mean that all of your databases are using dBase. And I don't think PHP has any built-in functionality for dBase. Once you find a method for connecting to the database then there is one more hurdle to over come; indexes.

MivaScript uses a proprietary index system, so any PHP script you create will not be able to use our indexes so you'll have to work a little harder to get the correct information out. Speaking of which, ONLY read from the databases. Do not try and update any database using PHP. It will put the indexes out of sync and cause a lot of problems when trying to use the store.

With all that doom and gloom out of the way, the way to use PHP inside of MivaScript would be via the <MvCALL> command. CALL the PHP script and let it do it's work. It should also be possible for the PHP to return back information that MivaScript can then use within it's own application.

I've heard of a number of examples where MivaScripts would MvCALL a perl/cgi script to get information and return results the MivaScript would display. Directory listings, accessing files outside of the Empresa "sandbox", and performing regex text manipulations are but a few examples.

Good luck if you take the plunge to get the two languages to work together.

/Scott

Brandon MUS
09-26-07, 10:02 PM
Just to throw this in there, there is a free "api" available called PHPXBase. I tried once to get it to work and wasn't able, but you can give it a shot and see if you have better luck.
I'm not sure what your reasoning is for staying with Miva 4, but Miva 5 is going to be a lot easier to work with -- especially if you have more fluent with PHP than MivaScript.

alexboyer
09-26-07, 11:45 PM
Just to throw this in there, there is a free "api" available called PHPXBase. I tried once to get it to work and wasn't able, but you can give it a shot and see if you have better luck.
I'm not sure what your reasoning is for staying with Miva 4, but Miva 5 is going to be a lot easier to work with -- especially if you have more fluent with PHP than MivaScript.
PHP has a function called dbase_open that worked for me when I had a client wanting to pull info from Miva 4.x's dBase files. Scott's right about not attempting to write to dBase... dBase is notorious for corrupting if more than one process attempts to write to it simultaneously in addition to the custom indices...

dbase_open()
dbase_numrecords()
dbase_get_record_with_names()
dbase_close()

Reference:
http://us.php.net/dbase

The only PITA is they don't have the ability to arbitrarily pull a row out like you can with MySQL so I ended up having to build a fairly substantial array out of the entire table and parse search parameters against it from there. It was far slower than MySQL, but surprisingly not as much as I had expected and it worked which is all I cared about since the client didn't want to upgrade to MySQL 5.

alexboyer
09-26-07, 11:49 PM
Hi,

In the Customer Account, I want to add 2 fields. One asks the customer if they want to participate in a Rewards program we are offering (they can say YES or NO). The other field asks them for the the e-mail of the person who referred them to our company.

When a customer checkout their order, I collect the information I need, namely, Customer name, e-mail, password, address, orders made (and the cost), etc. This is the kind of information I pull from Miva Merchant in order to update the Database and perform the necessary calculations. For example:

If customer B says they want to participate in the rewards program, and they were referred by customer A then i have to link B to A in a list. Then if B makes a purchase, I keep track of orders made. THen I perform the calculations that are necessary so that i can assign commissions to A who referred B to us.

I have already written all the code in PHP and designed a prototype for the system. the prototype works fine, but now i need to integrate it into my miva store. THis morning I actually bought a module called ADS External File Pro Module, to help me make use of my php scripts. I just installed it, but idon't know how it works yet....thats what i'm working on right now.

any suggestions or advice will be greatly appreciated!

Thanks

I've typically run the info through Javascript (I know, I know) and pull the info out of the s01_Customers table by referencing the cookie htscallerid that Miva creates.

Dee
10-01-07, 10:34 PM
I gave up trying to find good Miva 5 documentation and purchased the ADS External File Pro Module module so I could call out to PHP scripts. We have been using it with excellent results, for over a year.

My honest assessment is, if instead, I had tried to do all of this using Miva's languages and tools, I'd still be trying to pull it all together. (Given that I already knew PHP and had developed PHP classes tailored to our business logic for querying MySql databases.)

rob_the_yank
10-05-07, 05:19 PM
Thanks for info regarding the module. However, it only works in Miva 5 and we use 4. My boss figures if we're going to do as much work as 5 requires, we might as well jump straight in to PHP.

So anybody know of any modules that allow us to use PHP in Miva 4?

alexboyer
02-12-08, 01:10 AM
I developed a pretty simple process for wrapping a design template/php scripted page around the miva engine:

File:
miva_template.php


foreach($_REQUEST as $key=>$var)
{
$mvstr .= "&".urlencode($key)."=".urlencode($var);
}
$mvstr .= "&Session_ID=".$_COOKIE['htscallerid'];
include("http://www.domain.com/mm5/merchant.mvc?Store_Code=ST&fromphp=1".$mvstr);



You need to "touch" a Miva page prior to using this as the htscallerid cookie needs to be generated.

If I can figure out how to generate the cookie before Miva handles it, I'll post an update. Otherwise, the way I took care of it was to create a new page in Miva and fill it with nothing then access it through a small iframe or something.

It's what we're doing now on our site.

Brandon MUS
02-12-08, 05:44 PM
That's interesting. I didn't think you could pass an HTTP request via the include function. Did you have a unique php setup by chance?
Also, how are you handling the "touch" of the Miva screen?

alexboyer
02-12-08, 05:52 PM
It's fairly typical. Only minor tweaks I've done.

The touch was tricky, but what I did was create a new Page in Miva that contained nothing. Then I set that page as the src of an invisible, out of the way iframe (yeah yeah yeah :P) that's written from a file included in every other file. So no matter where they enter the site, they'll always have a Miva cookie generated. Really just a way for the user to hit the Miva engine and get that cookie.

Biffy
02-13-08, 07:01 AM
being able to include MVC within a PHP file... I just wonder why it can't the other way around.

You can. Use the HTML object tag to insert PHP pages and into Miva templates.

Brandon MUS
02-13-08, 05:30 PM
It's fairly typical. Only minor tweaks I've done.

The touch was tricky, but what I did was create a new Page in Miva that contained nothing. Then I set that page as the src of an invisible, out of the way iframe (yeah yeah yeah :P) that's written from a file included in every other file. So no matter where they enter the site, they'll always have a Miva cookie generated. Really just a way for the user to hit the Miva engine and get that cookie.
But wouldn't that iframe be loaded last (meaning all of the server side scripts would be processed without access to the MM5 cookie)? Another way to do it (without an iframe) would be to use file_get_contents(<your mm5 page>). This would "call the page", but wouldn't display the results. You can capture the results by saving it to a variable; which you can then manipulate and echo.

alexboyer
02-14-08, 06:17 PM
But wouldn't that iframe be loaded last (meaning all of the server side scripts would be processed without access to the MM5 cookie)? Another way to do it (without an iframe) would be to use file_get_contents(<your mm5 page>). This would "call the page", but wouldn't display the results. You can capture the results by saving it to a variable; which you can then manipulate and echo.
The iframe is just to touch Miva and generate a cookie. The next page visit, refresh or other bit of activity will carry over the cookie info. So you're right in that their first page view will not have access to the cookie. I'm not too worried about that though as it would be a rare occurance for us to get product add requests on the first page view.

My fear about doing it with file_get_contents is that PHP sends the request from the server, not the client's browser, which might result in shared htscallerid hashes. I'm sure you can imagine what would happen with that hehehe.

The iframe sends the page request to Miva from the client's browser so there's no chance of a shared hash.

Brandon MUS
02-14-08, 07:46 PM
Ah, you are correct. I guess technically your server would be receiving a cookie. The only thing I could think of better than an iframe would require JavaScript.

I like your idea though, much better than what I was doing :D

alexboyer
02-26-08, 06:01 PM
That's interesting. I didn't think you could pass an HTTP request via the include function. Did you have a unique php setup by chance?
Also, how are you handling the "touch" of the Miva screen?
I found out what the difference with this was.

Requests cannot be sent that way if you're including a file.
include($_SERVER['DOCUMENT_ROOT']."/path/to/file.mvc?somevar=true"); //will not work: Gives file not found error.

Requests can be sent that way if you're including a url.
include("http://www.domain.com/path/to/file.mvc?somevar=true"); //will work

Who knew? :p

Brandon MUS
02-26-08, 06:28 PM
Another cool tidbit to add on there is the same thing works with file_get_contents(), fopen(), and most other file handling functions in PHP. If you specify a HTTP wrapper in the file name, PHP will send a HTTP request including GET requests.

alexboyer
02-26-08, 06:30 PM
Oh nice, I hadn't considered that. That way you could actually plug it into a variable, huh? Sheesh, that opens up all kinds of stuff, thanks!!!

alexboyer
02-26-08, 06:35 PM
Yeah wow, that's way more powerful than my original concept.

alexboyer
03-17-08, 11:07 PM
So the new method would be:

foreach($_REQUEST as $key=>$var)
{
$mvstr .= "&".urlencode($key)."=".urlencode($var);
}
$mvstr .= "&Session_ID=".$_COOKIE['htscallerid'];
$mivaSrc = file_get_contents("http://www.domain.com/mm5/merchant.mvc?Store_Code=ST&fromphp=1".$mvstr);

Then you could plug in the entire page into the template:

$template = <<<EOT
<div>{$mivaSrc}</div>
EOT;

echo $template;