{"id":857,"date":"2015-06-13T21:32:42","date_gmt":"2015-06-13T15:32:42","guid":{"rendered":"http:\/\/promincproductions.com\/blog\/?p=857"},"modified":"2015-06-13T21:33:16","modified_gmt":"2015-06-13T15:33:16","slug":"run-php-script-node-js","status":"publish","type":"post","link":"https:\/\/promincproductions.com\/blog\/run-php-script-node-js\/","title":{"rendered":"Run PHP Scripts from Node JS"},"content":{"rendered":"<h2>The Problem<\/h2>\n<p>Due to the asynchronous only\u00a0design of ImageMagick within Node JS, I ran into a nightmare trying trying to code a recent web application. \u00a0You can not run ImageMagick synchronously\u00a0in NodeJS, I wasn&#8217;t able to get callbacks to work correctly, the<a href=\"https:\/\/github.com\/caolan\/async\" target=\"_blank\" data-lasso-id=\"393\" rel=\"noopener\"> npm\u00a0async series<\/a> method wasn&#8217;t working, and the nesting of ImageMagick functions wasn&#8217;t favorable. \u00a0Much of my code was using ImageMagick to check the properties of the image and do conditional functions based on the results.<\/p>\n<p>All that complaining said, the bigger frustration is that I already had most of this script written in PHP. \u00a0In PHP the script uses an object oriented approach which does allow for a synchronous flow and conditional checks. \u00a0So could I use this PHP script along side of my Node JS application?<\/p>\n<figure id=\"attachment_862\" aria-describedby=\"caption-attachment-862\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js.jpg\" rel=\"attachment wp-att-862\" data-lasso-id=\"394\" data-rel=\"lightbox-gallery-I7X2nLuX\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img decoding=\"async\" class=\"size-medium wp-image-862\" src=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js-500x363.jpg\" alt=\"How to call PHP from Node JS\" width=\"500\" height=\"363\" srcset=\"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js-500x363.jpg 500w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js-150x109.jpg 150w, https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js.jpg 600w\" sizes=\"(max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-862\" class=\"wp-caption-text\">If you are struggling with the asynchronous nature of Node JS and need to make a synchronous function call that PHP might be better suited for, you can simply utilize both languages. Make a call to a PHP script from Node JS with the exec command.<\/figcaption><\/figure>\n<p>A bit of digging and all of the solutions I was able to find suggested running Apache along side of Node JS, which would then allow you to call PHP via Apache. \u00a0But seriously, this makes no sense. \u00a0Calling a web server from a web server on the same machine? \u00a0Even worse, I wanted my Node JS web app to be called on port 80, so installing Apache will create a port conflict, etc.<\/p>\n<p>I want to give credit to <a href=\"http:\/\/www.scriptol.com\/javascript\/nodejs-php.php\" target=\"_blank\" data-lasso-id=\"395\" rel=\"noopener\">scriptol.com<\/a> for being the only reference I could find on how to beautifully blend Node JS with the synchronous methods of ImageMagick in PHP.<\/p>\n<h2>The Solution<\/h2>\n<h3>Setup a Child Process Dependency<\/h3>\n<pre><code>var\u00a0runner = require(\"child_process\");<\/code><\/pre>\n<h3>Call PHP via Child Process Dependency<\/h3>\n<p>Using the Node JS\u00a0<em><a href=\"https:\/\/nodejs.org\/api\/child_process.html#child_process_child_process_exec_command_options_callback\" target=\"_blank\" data-lasso-id=\"396\" rel=\"noopener\">exec command<\/a><\/em>, we can call to a PHP script.<\/p>\n<p>The exec command is essentially just calling a linux command directly. \u00a0So the first part of the command,\u00a0<em>php<\/em>, invokes the PHP application. \u00a0We then tell it what the path is to the PHP script, and lastly provide any arguments we want to pass to PHP. \u00a0In my case I wanted to pass multiple values, so I comma separated them.<\/p>\n<p>The exec command will return any errors thrown, the output of the PHP script, and any additional errors.<\/p>\n<p>In your PHP script, the argument you pass (argsString) will be available in the <em>$argv<\/em> array at position 1. \u00a0Split that string on it&#8217;s delimiter, a comma in my example, and you have an array of the values you passed to PHP. \u00a0Any\u00a0<em>echo<\/em> calls will be returned to the output (phpResponse) to Node JS.<\/p>\n<h3>Node JS Script:<\/h3>\n<p>&nbsp;<\/p>\n<pre><code>\r\nvar phpScriptPath = \"path\/to\/your\/php\/script.php\";\r\nvar argsString = \"value1,value2,value3\";\r\nrunner.exec(\"php \" + phpScriptPath + \" \" +argsString, function(err, phpResponse, stderr) {\r\n if(err) console.log(err); \/* log error\u00a0*\/\r\nconsole.log( phpResponse );\r\n});<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>PHP Script:<\/h3>\n<pre><code>&lt;?php\r\n$params = explode(\",\", $argv[1]);\r\necho $params[0] .\" - \" . $params[1] . \" - \" . $params[2];\r\n?&gt;<\/code><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>","protected":false},"excerpt":{"rendered":"<p>The Problem Due to the asynchronous only\u00a0design of ImageMagick within Node JS, I ran into a nightmare trying [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":862,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wprm-recipe-roundup-name":"","wprm-recipe-roundup-description":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[52,13,5],"tags":[],"class_list":["post-857","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs","category-php-development","category-website-development"],"jetpack_featured_media_url":"https:\/\/promincproductions.com\/blog\/wp-content\/uploads\/2015\/06\/make-php-calls-from-node-js.jpg","jetpack_shortlink":"https:\/\/wp.me\/p4BbcR-dP","jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/comments?post=857"}],"version-history":[{"count":6,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/857\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/posts\/857\/revisions\/864"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media\/862"}],"wp:attachment":[{"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/media?parent=857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/categories?post=857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/promincproductions.com\/blog\/wp-json\/wp\/v2\/tags?post=857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}