将终端cURL调用转换为PHP cURL请求

时间:2014-12-23 06:40:00

标签: php rest curl

我正在尝试将PHP的cURL请求发送到ExpressPigeon.com RESTFUL API。

文档说这是使用cURL命令在列表中创建联系人的方法:

curl -X POST -H "X-auth-key: 00000000-0000-0000-0000-000000000000" \
 -H "Content-type: application/json" \
 -d '{"list_id": 11,
      "contacts": [
         {"email": "john@doe.net",
          "first_name":"John",
          "last_name": "Doe"
         },
         {"email": "jane@doe.net",
          "first_name":"Jane",
          "last_name": "Doe"
         }] }' \
 https://api.expresspigeon.com/contacts

这就是我的所作所为:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.expresspigeon.com/contacts');
$fields = array(
            'list_id' => $this->list_code,
            'contacts' => array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name'])
        );
$this->http_build_query_for_curl($fields); //This generates the $this->post_data

curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $this->post_data);

$headers = array(
            'X-auth-key: '.$this->api_key,
            'Content-type: application/json',
            'Content-Length: '.strlen(serialize($post))
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$res = (array) json_decode(curl_exec($ch));
curl_close($ch);
print_r($res);

function http_build_query_for_curl( $arrays, $prefix = null )
{
    if ( is_object( $arrays ) ) {
        $arrays = get_object_vars( $arrays );
    }

    foreach ( $arrays AS $key => $value ) {
        $k = isset( $prefix ) ? $prefix . '['.$key.']' : $key;
        if ( is_array( $value ) OR is_object( $value )  ) {
            $this->http_build_query_for_curl( $value, $k );
        } else {
            $this->post_data[$k] = $value;
        }
    }
}

我的结果虽然如此:

Array
(
    [status] => error
    [code] => 400
    [message] => required Content-type: application/json
)

2 个答案:

答案 0 :(得分:1)

您需要再添加一个级别的数组嵌套来实现所需的JSON输出,以便contacts是一个数组而不是一个对象:

$fields = array(
        'list_id' => $this->list_code,
        'contacts' => array(array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name']))
    );

然后使用json_encode;您添加调试打印输出并与您需要的内容进行比较

答案 1 :(得分:0)

支持刚刚回复,这是工作代码。我试过了,它的确有效!我唯一没做的就是cURL上的SSL参数。感谢Hans Z对我的联系人阵列的修改。

$fields = array(
    'list_id' => $this->list_code,
    'contacts' => array(array('email' => $param['email'], 'first_name' => $param['first_name'], 'last_name' => $param['last_name']))
);
$requestBody = json_encode($fields);
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "https://api.expresspigeon.com/contacts"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST,           true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','X-auth-key:'.$this->api_key));
$result = curl_exec($ch); 
curl_close($ch);