How can I convert a PHP application in Bluemix to

2019-08-14 19:40发布

问题:

I have been following this tutorial: "Build and deploy a REST API on IBM Bluemix with PHP and MySQL."

The tutorial uses mySQL. I want to use the SQL Database service, which uses DB2, I think. The tutorial implements a REST API. When I try making a REST call using these links I get nothing in return.

For example, here's how I use my Bluemix application's REST API:

  • http://products-api-db2-test2-5.mybluemix.net/v1/products.json
  • http://products-api-db2-test2-5.mybluemix.net/v1/products

I have downloaded the source code from the link below and edited the manifest.yml to give it a unique name and host. I have also attempted to edit the index.php to connect to the SQL Database service as opposed to the mySQL database service.

My code repository is here: vvaswani | products-api.

After uploading the source to Bluemix, I create a table in the database using the following query:

CREATE TABLE products 
 (
  id INT,
  name VARCHAR(5),
  price decimal(5,2)
  );

I also populate the database with with the following query:

INSERT INTO products (id, name, price) VALUES
 (1, 'Garden spade', 15.99),
 (2, 'Cotton hammock', 54.50),
 (3, 'Single airbed', 35.49);

I bind the database to the app I just uploaded (which is the source code from the tutorial). But as I said above, when I try accessing the REST endpoint,s I get a blank page. I think I should be seeing JSON in my web browser. So I think there is something wrong with my connection between my app and database.

In the index.php file, there is a section specifying the connection to the mySQL database.

// get MySQL service configuration from BlueMix
 $services = getenv("VCAP_SERVICES");
 $services_json = json_decode($services, true);
 $mysql_config = $services_json["mysql-5.5"][0]["credentials"];
 $db = $mysql_config["name"];
 $host = $mysql_config["host"];
 $port = $mysql_config["port"];
 $username = $mysql_config["user"];
 $password = $mysql_config["password"];

 // initialize Eloquent ORM
 $capsule = new Capsule;

 // use for BlueMix development
 $capsule->addConnection(array(
     'driver'    => 'mysql',
     'host'      => $host,
     'port'      => $port,
     'database'  => $db,
     'username'  => $username,
     'password'  => $password,
     'charset'   => 'utf8',
     'collation' => 'utf8_unicode_ci',
     'prefix'    => ''
 ));

I think I need to edit this but I am not exactly sure how to. I have tried a few things. For example changing the $services_json["mysql-5.5"][0] to $services_json["sqldb"][0]. I also tried changing the driver from driver' => 'mysql' to 'driver' => 'sqldb'.

I think the credentials are probably important so here they are. mySQL credentials:

{
   "mysql-5.5": [
     {
       "name": "mysql-338",
       "label": "mysql-5.5",
       "plan": "100",
       "credentials": {
         "name": "d80979f54940b447ab178d6b2ea42a7f6",
         "hostname": "198.11.234.66",
         "host": "198.11.234.66",
         "port": 3307,
         "user": "uyhDjPNyxI357",
         "username": "uyhDjPNyxI357",
         "password": "XXXXXXXX",
         "uri": "mysql://uyhDjPNyxI357:pZoFBeIIg0i16@198.11.234.66:3307/d80979f54940b447ab178d6b2ea42a7f6"
       }
     }
   ]
 }

SQL Database credentials:

{
   "sqldb": [
     {
       "name": "SQL Database-78",
       "label": "sqldb",
       "plan": "sqldb_free",
       "credentials": {
         "port": 50000,
         "db": "SQLDB",
         "username": "user04128",
         "host": "75.126.155.153",
         "hostname": "75.126.155.153",
         "jdbcurl": "jdbc:db2://75.126.155.153:50000/SQLDB",
         "uri": "db2://user04128:dAxOPnUHTJ6G@75.126.155.153:50000/SQLDB",
         "password": "XXXXXXXX"
       }
     }
   ]
 }

It seems that the mySQL database credentials have a name attribute that is used in the index to reference the database. Where as the SQL Database credentials have a db property. So I might try playing around with that in the meantime.

Sorry for the long post. All of this is new too me.

Cheers.

回答1:

If you want to run it on Bluemix (via Cloud Foundry) you will need a PHP buildpack with DB2 driver support. This one should work: https://github.com/ibmdb/db2heroku-buildpack-php.

Working sample here:

Tip: to troubleshoot problems like this try the cf logs "APP_NAME" --recent command. Read more here



回答2:

You will need to install a driver for the DB2 database engine (which provides the SQLDB service) for your PHP framework.

https://github.com/cooperl22/laravel-db2

You can then follow the instructions to use the correct driver with the new credentials to connect to the database.



回答3:

The 'driver' you would use would be 'ibmdb2' rather than 'sqldb'. The SQLDB service in Bluemix is actually IBM DB2 and so you will need to use a ibmdb2 driver.

Having said that I don't believe eloquent (illuminate) supports DB2 so either you need to write your own connector in illuminate or you could use something like doctrine which has DB2 support I believe.