Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ruby HttpParty post请求_Php_Ruby_Http_Post_Httparty - Fatal编程技术网

Php Ruby HttpParty post请求

Php Ruby HttpParty post请求,php,ruby,http,post,httparty,Php,Ruby,Http,Post,Httparty,我有可用的php代码 <?php $ch = curl_init("https://myurl/api/add_lead"); $first_name = $_POST["name"]; $phone = $_POST["phone"]; $email = $_POST["email"]; $ipaddress = $_SERVER['REMOTE_ADDR']; curl_setopt($ch,CURLOPT_RETURNTRAN

我有可用的php代码

   <?php
    $ch = curl_init("https://myurl/api/add_lead");

    $first_name = $_POST["name"];
    $phone = $_POST["phone"];
    $email = $_POST["email"];
    $ipaddress = $_SERVER['REMOTE_ADDR'];

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1120);

    curl_setopt($ch,CURLOPT_POSTFIELDS,"first_name=$first_name&phone=$phone&email=$email&ipaddress=$ipaddress");
    curl_setopt($ch,CURLOPT_HTTPHEADER,["Content-Type:application/x-www-form-urlencoded; charset=utf-8"]);

    curl_setopt($ch,CURLOPT_TIMEOUT,60);
    $result = curl_exec($ch);

    curl_close($ch);
    ?>
但是得到了
500
错误代码


如何正确执行?

请注意,在PHP代码中,您将传递一个字符串作为帖子正文。
在Ruby代码中,传递的是json

请尝试以下操作:

HTTParty.post("https://myurl/api/add_lead", {
  body: "first_name=test&phone=123456789&email=email@email.com&ipaddress=192.168.0.0",
  headers: {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'charset' => 'utf-8'
  }
})

对于
应用程序/x-www-form-urlencoded
内容类型,请使用
URI.encode\u www\u表单(您的数据)

HTTParty.post("https://myurl/api/add_lead", {
  body: "first_name=test&phone=123456789&email=email@email.com&ipaddress=192.168.0.0",
  headers: {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'charset' => 'utf-8'
  }
})
require 'uri'

...

data = { 
  first_name: "test", 
  phone: "123456789", 
  email: "email@email.com" , 
  ipaddress:'192.168.0.0'
}

authorization_string = "#{ENV['API_KEY']}:#{ENV['API_SECRET']}"
url = "https://myurl/api/add_lead"

response = HTTParty.post(url,
  body: URI.encode_www_form(data),
  headers: { 
    'Content-Type' => 'application/x-www-form-urlencoded', 
    'Authorization' => "Basic #{Base64.strict_encode64(authorization_string)}" 
  }
)

response_body = JSON.parse(response.body)