如何使用cURL

时间:2016-02-16 11:30:57

标签: c++ linux curl

我有一个基于Linux的嵌入式系统,并使用cURL库从ip camera拍摄快照。

我的相机提供了一些CGI功能,其中一个用于拍摄图像快照。

调用专用CGI功能后,摄像头成功发送拍照。

我将相机响应保存为jpg文件,到目前为止一切正常。

但是当网络连接断开时,我的代码会产生零大小的jpg文件。

我删除了零大小的图像,但如果curl_easy_perform(curl)调用返回错误,最好不保存图像。

是否可以在cURL中出现错误时禁用保存?

  const int maxNumberOfImages = 20;
  CURL *curl;
  CURLcode result;
  FILE *CURLOPT_WRITEFUNCTION_FILE;
  bool flag = true;

  string url = "http://admin:1234@";//needs refactory
  url.append(ip);
  url.append(":80/snapshot.jpg");
  DEBUG_PRINT(url);

  char actionDate[20];
  time_t now = time(NULL);
  strftime(actionDate, 20, "%Y-%m-%d %H:%M:%S", localtime(&now));

  char *directory = "/root/rootfs_frequent_rw/snapshots/";

  string fileName = directory;
  fileName.append(cam_name);
  fileName.append("___");
  fileName.append(actionDate);
  fileName.append(".jpg");

  createDirectoryIfNotExist(directory);

  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  CURLOPT_WRITEFUNCTION_FILE = fopen(fileName.c_str(), "wb");

  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, CURLOPT_WRITEFUNCTION_FILE);
    result = curl_easy_perform(curl);
    if(result != CURLE_OK){
        DEBUG_PRINT("ERROR\n");
        flag = false;
    }
    else
        DEBUG_PRINT("SUCCESS\n");
    curl_easy_cleanup(curl);
  }

  fclose(CURLOPT_WRITEFUNCTION_FILE);
  cx based embedded system and taking snapshots from an ip camera. url_global_cleanup();

  isFileEmpty(fileName.c_str()); //if file is zero sized delete it.

  if(flag)
        return ReturnValue::return_success;
  return ReturnValue::return_operation_failed; 

1 个答案:

答案 0 :(得分:1)

我通过使用CURLOPT_WRITEFUNCTION选项解决了这个问题。在从cURL文档更改示例代码(getinmemory)后,我得到了以下代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

struct MemoryStruct {
  char *memory;
  size_t size;
};

static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
  size_t realsize = size * nmemb;
  struct MemoryStruct *mem = (struct MemoryStruct *)userp;

  mem->memory = realloc(mem->memory, mem->size + realsize + 1);
  if(mem->memory == NULL) {
    /* out of memory! */ 
    printf("not enough memory (realloc returned NULL)\n");
    return 0;
  }

  memcpy(&(mem->memory[mem->size]), contents, realsize);
  mem->size += realsize;
  mem->memory[mem->size] = 0;

  return realsize;
}

int main(void)
{
  CURL *curl_handle;
  CURLcode res;
  FILE *pFile;
  struct MemoryStruct chunk;

  chunk.memory = malloc(1);  /* will be grown as needed by the realloc above */ 
  chunk.size = 0;    /* no data at this point */ 

  curl_global_init(CURL_GLOBAL_ALL);

  /* init the curl session */ 
  curl_handle = curl_easy_init();

  /* specify URL to get */ 
  curl_easy_setopt(curl_handle, CURLOPT_URL, "http://admin:1234@10.108.67.235:80/snapshot.jpg");

  /* send all data to this function  */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

  /* we pass our 'chunk' struct to the callback function */ 
  curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk);

  /* some servers don't like requests that are made without a user-agent
     field, so we provide one */ 
  curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");

  curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 1L);
  /* get it! */ 
  res = curl_easy_perform(curl_handle);

  /* check for errors */ 
  if(res != CURLE_OK) {
    fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  }
  else {
    /*
     * Now, our chunk.memory points to a memory block that is chunk.size
     * bytes big and contains the remote file.
     *
     * Do something nice with it!
     */ 

    printf("%lu bytes retrieved\n", (long)chunk.size);

    pFile = fopen("test.jpg", "wb");

    fwrite(chunk.memory, sizeof(char), (long)chunk.size, pFile);

    fclose(pFile);
  }

  /* cleanup curl stuff */ 
  curl_easy_cleanup(curl_handle);

  free(chunk.memory);

  /* we're done with libcurl, so clean it up */ 
  curl_global_cleanup();

  return 0;
}