【问题标题】:cURL GET request function is not returning a stringcURL GET 请求函数未返回字符串
【发布时间】:2017-11-13 06:07:52
【问题描述】:

我正在 OS X 中编写一个程序,该程序需要通过 PHP 脚本运行用户 HWID,然后它将回显一个值,该值将使用我的代码中的函数读取。

这个 PHP 脚本,主要检查发出 GET 请求的 IP,检查 IP 是否存在于表中,如果存在,则检查其他值,例如给定的 HWID 是否正确,或者用户是否是溢价,然后根据这个返回一个字符串。

这是这里的功能;

string pullResult(string hwid) {
    CURL* curl;
    string result;

    curl = curl_easy_init();
    std::string url = "removed/usercheck.php?hwid=" + hwid;
    curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

    result = curl_easy_perform(curl);

    return result;
}

这就是它的名字;

if(al.pullResult(hwid) == "no such user")
    cout << "no such user" << endl;

但是,使用此功能不起作用。我可以进入我的浏览器并发出 GET 请求,脚本会返回正确的字符串,但这只是不返回字符串。

现在,我知道我正在检查正确的字符串,因为我已经为这个字符串设置了所有结果。

编辑:我现在实际上已经阅读了手册,大声笑,并发现这不会返回字符串或从网页读取的内容,而是它自己的标志性东西。如果有人能告诉我我在函数中做错了什么,那就太好了。

【问题讨论】:

  • through a PHP script-> 你的 php 脚本到底在哪里?
  • 我刚刚编辑了,你看看。我不愿意发布完整的脚本,因为它可能会在以后危及安全性,因为这在某些概念中是“防泄漏”。
  • @JohnKap 它不返回string,而是返回char*。你应该记住 curl 是一个 c-API。
  • 啊,我忘了!所以我就走了;字符串结果 2 = 结果(字符);

标签: php c++ curl libcurl


【解决方案1】:

你可以使用这样的东西。它将指向std::string 对象的指针传递给回调函数,该回调函数将返回的数据附加到传入的std::string。错误检查的方式很少。在实际代码中,每个函数调用都应该检查错误。

#include <memory>
#include <stdexcept>
#include <string>
#include <curl/curl.h>

std::size_t http_write(void* buf, std::size_t size, std::size_t nmemb, void* userp)
{
    if(auto page_data_ptr = static_cast<std::string*>(userp))
    {
        page_data_ptr->append(static_cast<char*>(buf), size * nmemb);
        return size * nmemb;
    }

    return 0;
}

std::string http_get(std::string const& url)
{
    // I use a unique_ptr to automatically call the cleanup function
    // so we don't need to do it later after the call
    std::unique_ptr<CURL, void(*)(CURL*)> curl{curl_easy_init(), curl_easy_cleanup};

    curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, &http_write);
    curl_easy_setopt(curl.get(), CURLOPT_NOPROGRESS, 1L);
    curl_easy_setopt(curl.get(), CURLOPT_FOLLOWLOCATION, 1L);

    std::string page_data; // collect the results
    curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &page_data);
    curl_easy_setopt(curl.get(), CURLOPT_URL, url.c_str());

    CURLcode code = curl_easy_perform(curl.get());

    if(code != CURLE_OK)
        throw std::runtime_error(curl_easy_strerror(code));

    return page_data;
}

注意:在调用此函数之前不要忘记调用一次curl_global_init(CURL_GLOBAL_DEFAULT);

【讨论】:

  • 好吧,现在我明白了; libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: URL using bad/illegal format or missing URL 像这样创建 URL; std::string url = "https://antrax.pw/asdojaOSJdu2bashDL/jaDHISJdhajKDHkas/sjhadLIUSD2bjhasjdn/ajHDIUj2nsADJhmashD/usercheck.php?hwid=" + hwid;
  • 它也在扔这个if(code != CURLE_OK) throw std::runtime_error(curl_easy_strerror(code));
  • @JohnKap 我认为这是因为您的 URL 需要 SSL (https)。您需要为此设置其他选项。这个例子可以帮助curl.haxx.se/libcurl/c/ftpsget.html
  • 我已将此添加到 http_get 函数中; curl_easy_setopt(&amp;curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);,但我仍然遇到同样的错误。
  • @JohnKap 嗯。这个对我有用。您可以尝试使用更简单的 URL(例如 http://www.google.com)吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 2019-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多