Jump to content

Convert Ruby Code To PHP Help Please


btray77

Recommended Posts

Ruby Code:

{ "itemFilter" => { "keywords" => "milk" }}

my_input = { "itemFilter" => { "keywords" => "milk" }}.to_json # Turn hash input into JSON, store it in variable called "my_input"

@http = Net::HTTP.new("website.com") # Open connection to website.com

response_code, data = @http.post("/requests", "findItems=#{my_input}") # 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=#{input}", {'X-CUSTOM-HEADER' => 'MYCUSTOMCODE'})

my_hash = Crack::JSON.parse(data)

my_milk = my_hash["findItems"]["item"].first

 

Here's what I have so far, which might be totally wrong..  Any help would be appreciated.

 

 

<?PHP

$requestBody =json_encode(array("itemFilter" => array( "keywords" => "milk" )));

$headers = array (
		//set the keys
		'X-CUSTOM-HEADER: ' .'MYCUSTOMCODE',			
	);

	//initialise a CURL session
	$connection = curl_init();
	//set the server we are using (could be Sandbox or Production server)
	curl_setopt($connection, CURLOPT_URL, 'http://www.website.com/request/findItems=');

	//stop CURL from verifying the peer's certificate
	curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);

	//set the headers using the array of headers
	curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);

	//set method as POST
	curl_setopt($connection, CURLOPT_POST, 1);

	//set the XML body of the request
	curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);

	//set it to return the transfer as a string from curl_exec
	curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);

	//Send the Request
	$response = curl_exec($connection);

	//close the connection
	curl_close($connection);

	print_r($response);

 

Thanks

 

-Brad

Link to comment
https://forums.phpfreaks.com/topic/178540-convert-ruby-code-to-php-help-please/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.