1

In my previous question I had a problem with sending parameters over the command line to an PHP file. The PHP is sending an json back, but I only receive true or false.

Previous question: Rails, PHP and parameters

New problem: Ruby.rb

data = system('php public/jira.php param') 
puts data

PHP

$output = array(
    "total" => $total
);

echo json_encode($output);

EDIT:

Answer:

data = JSON.parse(data)

puts data['total'] #5

Community
  • 1
  • 1
Kevin Gorjan
  • 1,272
  • 1
  • 17
  • 34

2 Answers2

1

See this answer.

Basically you'll want to use:

data = `php public/jira.php param`
data JSON.parse(data)
puts data['total']

Instead of:

data = system('php public/jira.php param') 
puts data
Community
  • 1
  • 1
Mischa
  • 42,876
  • 8
  • 99
  • 111
  • Okay, I receive the content from my PHP, but rails doens't handle it as JSON. In Rails: jira['total'] puts out total and not a number? – Kevin Gorjan Apr 05 '12 at 11:52
  • Hmm... Now I receive the following error: undefined method `decode' for JSON:Module. I put require 'json' in the top, but it didn't help. – Kevin Gorjan Apr 05 '12 at 12:00
1

system() will return TrueClass or FalseClass and display output, try it on console .

I suggest , You can use the open method on any URL to call it, so you can call your PHP script using that:

require 'open-uri'

open('YOUR PHP SCRIPT PATH WITH PARAMETER') do |response|
  content = response.read
end

Or below link will help you .

6 Ways to Run Shell Commands in Ruby

Vik
  • 5,931
  • 3
  • 31
  • 38