请将Ruby代码转换为PHP帮助

请将Ruby代码转换为PHP帮助,php,ruby-on-rails,ruby,Php,Ruby On Rails,Ruby,Ruby代码: # 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 AP

Ruby代码:

  # 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代码:

$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);

请查看,并查看扩展。

发现了问题。。谢谢你的意见。我在不需要的地方放了一个尾随斜杠,而json_编码在编码周围放了括号,它不需要它们。

您需要帮助还是需要有人为您编写整个内容?我们会帮助您解决问题,但我们不会帮您完成工作。向我们展示你在这方面付出了一些努力,我们会在接下来的过程中帮助你。这就是我到目前为止所得到的$requestBody=json_encode(数组(“itemFilter”=>array(“关键字”=>milk”))$headers=array('X-CUSTOM-HEADER:MYCODE')$连接=curl_init();curl_setopt($connection,CURLOPT_URL');curl_setopt($connection,CURLOPT_HTTPHEADER,$headers);curl_setopt($connection,CURLOPT_POST,1);curl_setopt($connection,CURLOPT_POSTFIELDS,$requestBody);curl_setopt($connection,CURLOPT_RETURNTRANSFER,1)$响应=curl\u exec($connection);旋涡关闭($连接);打印($connectIt)不起作用。我猜我的URL或POST字段出错了,因为它可以看到我的自定义标题代码。编辑您的问题;添加代码、预期结果和实际结果。每行代码缩进4个空格以将其格式化为代码。