{"id":666,"date":"2017-03-04T13:11:30","date_gmt":"2017-03-04T18:11:30","guid":{"rendered":"https:\/\/www.padizio.com\/blog\/?p=666"},"modified":"2017-03-04T13:11:40","modified_gmt":"2017-03-04T18:11:40","slug":"push-it-real-good-but-with-less-stuff","status":"publish","type":"post","link":"https:\/\/www.padizio.com\/blog\/2017\/push-it-real-good-but-with-less-stuff\/","title":{"rendered":"Push it Real Good (but with less stuff)"},"content":{"rendered":"<p>A code post! How novel!<\/p>\n<p>Yesterday I was catching up on my blog reading and I ran across Casey Liss&#8217; <a href=\"https:\/\/www.caseyliss.com\/2017\/2\/25\/push-it-real-good\">Shell \u2192 Watch Notifications<\/a>. I love the idea &#8211; but it&#8217;s a bit of a kludge to put together as-is.<\/p>\n<p><!--more--><\/p>\n<p>You&#8217;ve got a shell script, running a php script and then curl, to a local Node server, which then proxies a call out to the Pushover API. Casey&#8217;s using the tools he knows (the hard part is really all in the javascript) and what he&#8217;s got works great.<\/p>\n<p>I just don&#8217;t want to run a server locally to ping another server &#8211; let&#8217;s just consolidate all this. At the same time, a lot of folks don&#8217;t realize that PHP can be used directly as a shell script. And, since PHP has a kitchen-sink mentality, you can use cURL directly from PHP. So let&#8217;s put it all together with the help of the best HTTP \/ API testing app ever, <a href=\"https:\/\/paw.cloud\">Paw<\/a>!<\/p>\n<p>Here&#8217;s how I set up the initial request in Paw:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-667\" src=\"https:\/\/www.padizio.com\/blog\/assets\/Screen-Shot-2017-03-04-at-11.57.48-AM.png\" alt=\"Paw Pushover Setup\" width=\"100%\" srcset=\"https:\/\/www.padizio.com\/blog\/assets\/Screen-Shot-2017-03-04-at-11.57.48-AM.png 766w, https:\/\/www.padizio.com\/blog\/assets\/Screen-Shot-2017-03-04-at-11.57.48-AM-300x260.png 300w\" sizes=\"(max-width: 766px) 100vw, 766px\" \/><\/p>\n<p>And here&#8217;s the code that Paw generated for PHP:<\/p>\n<pre><code>&lt;?php\n\n\/\/ Get cURL resource\n$ch = curl_init();\n\n\/\/ Set url\ncurl_setopt($ch, CURLOPT_URL, 'https:\/\/api.pushover.net\/1\/messages.json');\n\n\/\/ Set method\ncurl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');\n\n\/\/ Set options\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n\n\/\/ Set headers\ncurl_setopt($ch, CURLOPT_HTTPHEADER, [\n  \"Content-Type: application\/x-www-form-urlencoded; charset=utf-8\",\n ]\n);\n\/\/ Create body\n$body = [\n  \"message\" =&gt; \"This is a test message\",\n  \"title\" =&gt; \"Done\",\n  \"token\" =&gt; \"your-app-token\",\n  \"user\" =&gt; \"your-user-token\",\n  ];\n$body = http_build_query($body);\n\n\/\/ Set body\ncurl_setopt($ch, CURLOPT_POST, 1);\ncurl_setopt($ch, CURLOPT_POSTFIELDS, $body);\n\n\/\/ Send the request &amp; save response to $resp\n$resp = curl_exec($ch);\n\nif(!$resp) {\n  die('Error: \"' . curl_error($ch) . '\" - Code: ' . curl_errno($ch));\n} else {\n  echo \"Response HTTP Status Code : \" . curl_getinfo($ch, CURLINFO_HTTP_CODE);\n  echo \"\\nResponse HTTP Body : \" . $resp;\n}\n\n\/\/ Close request to clear up some resources\ncurl_close($ch);\n<\/code><\/pre>\n<p>Awesome. Now we&#8217;ve got a PHP script that can send a hard-coded message &#8211; assuming, of course, that you replace <code>your-app-token<\/code> and <code>your-user-token<\/code> with your relevant Pushover account details. Let&#8217;s adapt this into something we can run from the shell and pass arguments into.<\/p>\n<p>First up, I&#8217;m going to paste the above into a file called <code>finished<\/code>. For me, I put it in my <code>~\/bin<\/code> directory, because I&#8217;ve got that added to my <code>$PATH<\/code> &#8211; meaning I&#8217;ll be able to run this script from anywhere. While I&#8217;m there, I&#8217;ll do a quick <code>chmod +x<\/code> on it in the shell, so it becomes executable.<\/p>\n<p>At the top of my new <code>finished<\/code> script, I&#8217;ll put the magic incantation that helps your shell know this is PHP:<\/p>\n<pre><code>#!\/usr\/bin\/env php\n&lt;?php\n<\/code><\/pre>\n<p>That hash-bang line tells the shell to fire up the PHP interpreter. After that, the remaining PHP file gets executed automatically. Although, PHP being PHP, you still need the <code>&lt;?php<\/code> in order for the interpreter to, well, interpret. Otherwise it will just spit out your code back to you as plain text. Try it, it&#8217;s hilarious.<\/p>\n<p>Now you&#8217;ll be able to run <code>finished<\/code> on the command line and get your hard-coded message. Let&#8217;s take the arguments from the command line to turn that into the message. In a PHP shell script, <code>$argv<\/code> will be automatically available as an array of all the arguments passed into the script &#8211; including, up front, the full path to the script itself. Here&#8217;s how we turn that into the <code>$message<\/code> string at the top of the file:<\/p>\n<pre><code>$script_path = array_shift($argv); \/\/ This pops the script path off the front of the $argv array\n$message = implode(' ', $argv); \/\/ Concatenates the array together with spaces\n<\/code><\/pre>\n<p>And then I modify the <code>$body<\/code> array that Paw gave us:<\/p>\n<pre><code>...\n$body = [\n  \"message\" =&gt; $message,\n...\n<\/code><\/pre>\n<p>And that&#8217;s it! Now you can run:<\/p>\n<pre><code>&gt; some --long-running=task; finished The long running task finished\n<\/code><\/pre>\n<p>Side note: I don&#8217;t call the script <code>done<\/code> because unlike Casey, I use bash, and in bash <code>done<\/code> is a control keyword &#8211; so bash just won&#8217;t run any script called <code>done<\/code> unless I always give it the full path. Meh.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A code post! How novel! Yesterday I was catching up on my blog reading and I ran across Casey Liss&#8217; Shell \u2192 Watch Notifications. I love the idea &#8211; but it&#8217;s a bit of a kludge to put together as-is.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-666","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/posts\/666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/comments?post=666"}],"version-history":[{"count":13,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/posts\/666\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/media?parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/categories?post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.padizio.com\/blog\/wp-json\/wp\/v2\/tags?post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}