windows系统MySQL支持调用http接口(mysql-udf-http)

Windows平台MySQL调用外部http接口详解

            • Linux下mysql-udf-http
            • windows下mysql-udf-http

Linux下mysql-udf-http
  1. mysql-udf-http在Linux下编译安装,MySQL数据库创建函数后,可使用http_post或http_get调用外部接口。
windows下mysql-udf-http
  1. mysql-udf-http.c 此链接可下载包含了关于http_get以及http_post方法,可通过
    visual studio编译生成dll文件。
    windows系统MySQL支持调用http接口(mysql-udf-http)_第1张图片
    注意图中使用Windows常用工具创建win32位可生成dll的项目
    windows系统MySQL支持调用http接口(mysql-udf-http)_第2张图片
    注意此处必须选择dll
    windows系统MySQL支持调用http接口(mysql-udf-http)_第3张图片
    导入下载的.c文件,此文件是之前在网络上经过我大量筛选所得,需要根据自身环境以及使用情况去修改源文件,如遇到源文件一直报错编译不通过,可查阅官网资料,或留言一起讨论。

以下是题外话:
一般情况下原文中我不会贴源代码,(主要太影响别人阅读,都是说到哪儿,截取那部分,而且我写博客的版式不太好,但是我觉的我把我知道的技术分享给需要的人才是最重要的,)一般都是放附件或者网盘,评论区留邮箱也会发附件等等,但之前我一篇文章中分段贴了源码有人竟然说我的附件不能用,我看了下载记录他并没有下载,想着让我给他发原件,我就呵呵了,你要是没有积分,留个邮箱我一般看见了也会直接发附件的,技术是一个分享过程,而不是在别人评论区瞎喊,旺柴,你记住了么

最后贴出源文件:

#include 
#include 

#include 
#include 
#include "mysql-udf-http.h"

my_bool http_get_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
char *http_get(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
void http_get_deinit(UDF_INIT *initid);

my_bool http_post_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
char *http_post(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
void http_post_deinit(UDF_INIT *initid);

my_bool http_put_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
char *http_put(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
void http_put_deinit(UDF_INIT *initid);

my_bool http_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
char *http_delete(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
void http_delete_deinit(UDF_INIT *initid);

static void *myrealloc(void *ptr, size_t size)
{
     
  /* There might be a realloc() out there that doesn't like reallocing
     NULL pointers, so we take care of it here */
  if (ptr)
    return realloc(ptr, size);
  else
    return malloc(size);
}

static size_t
result_cb(void *ptr, size_t size, size_t nmemb, void *data)
{
     
  size_t realsize= size * nmemb;
  struct st_curl_results *res= (struct st_curl_results *)data;

  res->result= (char *)myrealloc(res->result, res->size + realsize + 1);
  if (res->result)
  {
     
    memcpy(&(res->result[res->size]), ptr, realsize);
    res->size += realsize;
    res->result[res->size]= 0;
  }
  return realsize;
}

/* ------------------------HTTP GET----------------------------- */

my_bool http_get_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
     
  st_curl_results *container;

  if (args->arg_count != 1)
  {
     
    strncpy(message,
            "one argument must be supplied: http_get('').",
            MYSQL_ERRMSG_SIZE);
    return 1;
  }

  args->arg_type[0]= STRING_RESULT;

  initid->max_length= CURL_UDF_MAX_SIZE;
  container= (st_curl_results *)malloc(sizeof(st_curl_results));

  initid->ptr= (char *)container;

  return 0;
}

char *http_get(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error)
{
     
  CURLcode retref;
  CURL *curl;
  st_curl_results *res= (st_curl_results *)initid->ptr;

  curl_global_init(CURL_GLOBAL_ALL);
  curl= curl_easy_init();

  res->result= NULL;
  res->size= 0;

  if (curl)
  {
     
    curl_easy_setopt(curl, CURLOPT_URL, args->args[0]);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, result_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)res);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "mysql-udf-http/1.0");
    retref= curl_easy_perform(curl);
    if (retref) {
     
      fprintf(stderr, "error\n");
      strcpy(res->result,"");
      *length= 0;
    }
  }
  else
  {
     
    strcpy(res->result,"");
    *length= 0;
  }
  curl_easy_cleanup(curl);
  *length= res->size;
  return ((char *) res->result);
}

void http_get_deinit(UDF_INIT *initid)
{
     
  /* if we allocated initid->ptr, free it here */
  st_curl_results *res= (st_curl_results *)initid->ptr;

  free(res->result);
  free(res);
  return;
}

/* ------------------------HTTP POST----------------------------- */

my_bool http_post_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
     
  st_curl_results *container;

  if (args->arg_count != 2)
  {
     
    strncpy(message,
            "two arguments must be supplied: http_post('','').",
            MYSQL_ERRMSG_SIZE);
    return 1;
  }

  args->arg_type[0]= STRING_RESULT;

  initid->max_length= CURL_UDF_MAX_SIZE;
  container= (st_curl_results *)malloc(sizeof(st_curl_results));

  initid->ptr= (char *)container;

  return 0;
}

char *http_post(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error)
{
     
  CURLcode retref;
  CURL *curl;
  st_curl_results *res= (st_curl_results *)initid->ptr;

  curl_global_init(CURL_GLOBAL_ALL);
  curl= curl_easy_init();

  res->result= NULL;
  res->size= 0;

  if (curl)
  {
     
    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Expect:");  
  
    curl_easy_setopt(curl, CURLOPT_URL, args->args[0]);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, result_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)res);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "mysql-udf-http/1.0");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, args->args[1]);
    retref= curl_easy_perform(curl);
    if (retref) {
     
      fprintf(stderr, "error\n");
      strcpy(res->result,"");
      *length= 0;
    }
  }
  else
  {
     
    strcpy(res->result,"");
    *length= 0;
  }
  curl_easy_cleanup(curl);
  *length= res->size;
  return ((char *) res->result);
}

void http_post_deinit(UDF_INIT *initid)
{
     
  /* if we allocated initid->ptr, free it here */
  st_curl_results *res= (st_curl_results *)initid->ptr;

  free(res->result);
  free(res);
  return;
}

/* ------------------------HTTP PUT----------------------------- */

my_bool http_put_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
     
  st_curl_results *container;

  if (args->arg_count != 2)
  {
     
    strncpy(message,
            "two arguments must be supplied: http_put('','').",
            MYSQL_ERRMSG_SIZE);
    return 1;
  }

  args->arg_type[0]= STRING_RESULT;

  initid->max_length= CURL_UDF_MAX_SIZE;
  container= (st_curl_results *)malloc(sizeof(st_curl_results));

  initid->ptr= (char *)container;

  return 0;
}

char *http_put(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error)
{
     
  CURLcode retref;
  CURL *curl;
  st_curl_results *res= (st_curl_results *)initid->ptr;

  curl_global_init(CURL_GLOBAL_ALL);
  curl= curl_easy_init();

  res->result= NULL;
  res->size= 0;

  if (curl)
  {
     
    struct curl_slist *chunk = NULL;
    chunk = curl_slist_append(chunk, "Expect:");  
  
    curl_easy_setopt(curl, CURLOPT_URL, args->args[0]);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, result_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)res);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "mysql-udf-http/1.0");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, args->args[1]);
    retref= curl_easy_perform(curl);
    if (retref) {
     
      fprintf(stderr, "error\n");
      strcpy(res->result,"");
      *length= 0;
    }
  }
  else
  {
     
    strcpy(res->result,"");
    *length= 0;
  }
  curl_easy_cleanup(curl);
  *length= res->size;
  return ((char *) res->result);
}

void http_put_deinit(UDF_INIT *initid)
{
     
  /* if we allocated initid->ptr, free it here */
  st_curl_results *res= (st_curl_results *)initid->ptr;

  free(res->result);
  free(res);
  return;
}

/* ------------------------HTTP DELETE----------------------------- */

my_bool http_delete_init(UDF_INIT *initid, UDF_ARGS *args, char *message)
{
     
  st_curl_results *container;

  if (args->arg_count != 1)
  {
     
    strncpy(message,
            "one arguments must be supplied: http_delete('').",
            MYSQL_ERRMSG_SIZE);
    return 1;
  }

  args->arg_type[0]= STRING_RESULT;

  initid->max_length= CURL_UDF_MAX_SIZE;
  container= (st_curl_results *)malloc(sizeof(st_curl_results));

  initid->ptr= (char *)container;

  return 0;
}

char *http_delete(UDF_INIT *initid, UDF_ARGS *args,
                __attribute__ ((unused)) char *result,
               unsigned long *length,
                __attribute__ ((unused)) char *is_null,
                __attribute__ ((unused)) char *error)
{
     
  CURLcode retref;
  CURL *curl;
  st_curl_results *res= (st_curl_results *)initid->ptr;

  curl_global_init(CURL_GLOBAL_ALL);
  curl= curl_easy_init();

  res->result= NULL;
  res->size= 0;

  if (curl)
  {
      
    curl_easy_setopt(curl, CURLOPT_URL, args->args[0]);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, result_cb);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)res);
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "mysql-udf-http/1.0");
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
    retref= curl_easy_perform(curl);
    if (retref) {
     
      fprintf(stderr, "error\n");
      strcpy(res->result,"");
      *length= 0;
    }
  }
  else
  {
     
    strcpy(res->result,"");
    *length= 0;
  }
  curl_easy_cleanup(curl);
  *length= res->size;
  return ((char *) res->result);
}

void http_delete_deinit(UDF_INIT *initid)
{
     
  /* if we allocated initid->ptr, free it here */
  st_curl_results *res= (st_curl_results *)initid->ptr;

  free(res->result);
  free(res);
  return;
}

你可能感兴趣的:(mysql-udf-http,mysql,windows)