在C语言中,可以使用网络编程库libcurl来向网址php发起指定内容的get请求。具体代码如下:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    char *url = "http://example.com/php_script.php?param1=value1&param2=value2";
    curl_easy_setopt(curl, CURLOPT_URL, url);
    res = curl_easy_perform(curl);
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));
    curl_easy_cleanup(curl);
  }
  return 0;
}

解释:

  1. 引入头文件<stdio.h><curl/curl.h>
  2. 定义main函数。
  3. 初始化CURL对象指针curl
  4. 如果curl指针不为空,则设置请求的URL地址和参数。
  5. 发起GET请求并获取响应结果。
  6. 如果请求失败,则输出错误信息。
  7. 清除CURL对象指针curl
  8. 返回0。

需要注意的是,需要在编译时链接libcurl库,可以在命令行中添加-lcurl参数。


原文地址: https://www.cveoy.top/t/topic/M9V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录