curl PHP:PHP手册-Manual

agyaoht7  于 6个月前  发布在  PHP
关注(0)|答案(1)|浏览(71)

我正在使用cURL进行API调用,下面是我的代码:

$data = array(
            'r'                          =>  'create',
            '_object'                    =>  'Question',
            '_api_key'                   =>  '........',
            '_user_id'                   =>  $creator_id,
            '_subtype_id'                =>  $type_id,
            '_subtopic_id'               =>  $subtopic_id,
            '_title'                     =>  $title,
            '_description'               =>  $description,             
            '_encoded_xml'               =>  $main_xml
        );
     
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,"http://urlhere/v1/resources/objects");
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
        $server_output = curl_exec($ch);    
        curl_close ($ch);
        print_r($server_output);

字符串
我希望这些参数将作为GET而不是POST请求发送,我怎么能做到这一点,请咨询

ifmq2ha2

ifmq2ha21#

CURLOPT_POSTFIELDS用于POST请求的主体(有效负载)。对于GET请求,有效负载是URL的一部分。
您只需要使用需要发送的参数(如果有的话)构造URL,并删除cURL的其他选项。

$data = array(
            'r'             =>  'create',
            '_object'       =>  'Question',
            '_api_key'      =>  '........',
            '_user_id'      =>  $creator_id,
            '_subtype_id'   =>  $type_id,
            '_subtopic_id'  =>  $subtopic_id,
            '_title'        =>  $title,
            '_description'  =>  $description,             
            '_encoded_xml'  =>  $main_xml
        );

        $send_data = http_build_query($data);
     
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL,"http://urlhere/v1/resources/objects?" . $send_data);
        // curl_setopt($ch, CURLOPT_POST, 1);
        // curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
        
        $server_output = curl_exec($ch);    
        curl_close ($ch);
        print_r($server_output);

字符串

相关问题