使用PHP通过CURL发布对象

时间:2013-03-19 22:00:33

标签: php api rest curl

我们正在尝试发布到其中一个密钥中需要“POST对象”的其他API端点。在javascript / jquery中,这很好用。但是,在PHP中使用CURL时,端点不会在此处接收对象(称为“组件”):

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_URL, 'http://api.sitesitesite.com');

    $comps = array('slug' => "xyz",
                   'visble' => 1,
                   'color' => "xyz",
                   'shape' => "xyz",
                   'version' => "2",
            );

    $post_args = array();
    $post_args['components'] = $comps;
    $post_args['id'] = $id;

    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_args);

    $result = curl_exec($ch);

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

1 个答案:

答案 0 :(得分:2)

摆脱“CURLOPT_CUSTOMREQUEST”并使用“CURLOPT_POST”

curl_setopt($ch, CURLOPT_POST, true);