views:

261

answers:

2

Ruby Code:

  # Turn hash input into JSON, store it in variable called "my_input"
my_input = { "itemFilter" => { "keywords" => "milk" }}.to_json 
  # Open connection to website.com
@http = Net::HTTP.new("website.com")
  # Post the request to our API, with the "findItems" name and our JSON from above as the value
response_code, data = @http.post("/requests", "findItems=#{my_input}", 
                                 {'X-CUSTOM-HEADER' => 'MYCUSTOMCODE'}) 
my_hash = Crack::JSON.parse(data)
my_milk = my_hash["findItems"]["item"].first

PHP code:

$requestBody = json_encode(array("itemFilter" => array( "keywords" => "milk" ))); 
$headers = array ('X-CUSTOM-HEADER: MYCODE'); 
$connection = curl_init(); 
curl_setopt($connection, CURLOPT_URL, 'website.com/request/findItems='); 
curl_setopt($connection, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($connection, CURLOPT_POST, 1); 
curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody); 
curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); 
$response = curl_exec($connection); 
curl_close($connection); 
print_r($response);
+1  A: 

Take a look at json_encode, json_decode and the cURL extension.

outis
This is what I've got so far.$requestBody =json_encode(array("itemFilter" => array( "keywords" => "milk" )));$headers = array ('X-CUSTOM-HEADER: MYCODE');$connection = curl_init(); curl_setopt($connection, CURLOPT_URL, 'http://www.website.com/request/findItems='); curl_setopt($connection, CURLOPT_HTTPHEADER, $headers); curl_setopt($connection, CURLOPT_POST, 1); curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody); curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($connection); curl_close($connection);print_r($connect
Brad
It doesn't work.. I'm guessing I'm going the URL or POST fields wrong, because it sees my custom header code just fine.
Brad
Edit your question; add your code, the expected result and actual result. Indent each line of code with 4 spaces to format it as code.
outis
A: 

Found the problem.. Thanks for the input. I put a trailing slash where it was not required, and the json_encode was putting brackets around the encoding, and it didn't need them.

Brad