简体   繁体   English

在CodeIgniter中使用Curl:如何使用post方法发送数据

[英]Using Curl in CodeIgniter : How to send data using post method

Not going to succeed when trying to send data using post method. 尝试使用post方法发送数据时不会成功。 Never used it before. 以前从未使用过。

This is code of executing curl: 这是执行curl的代码:

$url = LICENSE_URL."validate_system_key/validate_key/";
        //url-ify the data for the POST
        $data['system_key']='A8Z0-X1N7-S1V2-Y1I5';
        $data['domain']='http://example.com';
            $fields_string = '';
            foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
            $fields_string = rtrim($fields_string,'&');


            //open connection
            $ch = curl_init();

            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_POST,count($data));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); # timeout after 10 seconds, you can increase it
           //curl_setopt($ch,CURLOPT_HEADER,false);
            curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  # Set curl to return the data instead of printing it to the browser.

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

            //execute post
            $result = curl_exec($ch);

            //close connection
            curl_close($ch);
          $result=json_decode($result,TRUE);

And this is the Code On Client Url: 这是客户端URL上的代码:

public function validate_key(){
        $system_key=$_POST['system_key'];
        $domain=$_POST['domain'];
        $result['error']=3;
        echo json_encode($result);
}

Note: When I am not using POST method (sending data through url) evrything is fine. 注意:当我不使用POST方法(通过url发送数据)时,evrything很好。 Its all about POST method, not getting the mistake! 它全部与POST方法有关, 没有出错! Any kind of help will be appreciated! 任何帮助将不胜感激! Thanks. 谢谢。

Use array instead of String in data passed to string like below: 在传递给字符串的数据中使用array而不是String ,如下所示:

foreach($data as $key=>$value) { 
 $data[$key] = $value;
}
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

I hope this will work. 我希望这会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM