用post和get的方式将数据提交到api

来源:互联网 发布:股票量化投资软件 编辑:程序博客网 时间:2024/06/04 21:46


  function postData($url, $data)      

             {      
                $ch = curl_init();      
                $timeout = 300;       
                curl_setopt($ch, CURLOPT_URL, $url);     
                curl_setopt($ch, CURLOPT_REFERER, "http://www.jb51.net/");   //构造来路    
                curl_setopt($ch, CURLOPT_POST, true);      
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);      
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);      
                $handles = curl_exec($ch);      
                curl_close($ch);      
                return $handles;      

                    }   


将类转换为数组

 function object_array($array)
                {
                   if(is_object($array))
                   {
                    $array = (array)$array;
                   }
                   if(is_array($array))
                   {
                    foreach($array as $key=>$value)
                    {
                     $array[$key] = object_array($value);
                    }
                   }
                   return $array;
                }


用get方式获取api的数据

$fp = fopen($url, 'r');
            stream_get_meta_data($fp);                        //用fopen打开url, 以get方式获取内容:
            while(!feof($fp)) {
            $result .= fgets($fp, 1024);  
            }
            echo "url body: $result";

1 0
原创粉丝点击