Php cURL GET request函数未返回字符串
我正在用OSX编写一个程序,需要通过PHP脚本运行用户HWID,然后该脚本将回显一个值,该值将使用代码中的函数读取 这个PHP脚本基本上检查发出GET请求的IP,检查IP是否存在于表中,如果存在,然后检查其他值,例如给定的HWID是否正确,或者用户是否是premium,然后根据此返回字符串 这就是这里的函数Php cURL GET request函数未返回字符串,php,c++,curl,libcurl,Php,C++,Curl,Libcurl,我正在用OSX编写一个程序,需要通过PHP脚本运行用户HWID,然后该脚本将回显一个值,该值将使用代码中的函数读取 这个PHP脚本基本上检查发出GET请求的IP,检查IP是否存在于表中,如果存在,然后检查其他值,例如给定的HWID是否正确,或者用户是否是premium,然后根据此返回字符串 这就是这里的函数 string pullResult(string hwid) { CURL* curl; string result; curl = curl_easy_init(
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;
if(al.pullResult(hwid)=“没有这样的用户”)
你可以用这样的东西。它将指向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;
}
#包括
#包括
#包括
#包括
std::size\u t http\u write(void*buf,std::size\u t size,std::size\u t nmemb,void*userp)
{
如果(自动分页\u数据\u ptr=static\u cast(userp))
{
页面\u数据\u ptr->append(静态\u cast(buf),大小*nmemb);
返回大小*nmemb;
}
返回0;
}
std::string http_get(std::string const&url)
{
//我使用一个唯一的\u ptr自动调用cleanup函数
//所以我们不需要在电话之后再做
std::unique_ptr curl{curl_easy_init(),curl_easy_cleanup};
curl\u easy\u setopt(curl.get()、CURLOPT\u WRITEFUNCTION和http\u write);
curl\u easy\u setopt(curl.get(),CURLOPT\u NOPROGRESS,1L);
curl_easy_setopt(curl.get(),CURLOPT_FOLLOWLOCATION,1L);
std::string page_data;//收集结果
curl_easy_setopt(curl.get()、CURLOPT_WRITEDATA和page_data);
curl_easy_setopt(curl.get(),CURLOPT_URL,URL.c_str());
CURLcode code=curl\u easy\u perform(curl.get());
如果(代码!=CURLE_OK)
抛出std::运行时错误(curl\u easy\u strerror(代码));
返回页面数据;
}
注意:不要忘记调用curl\u global\u init(curl\u global\u默认值)在调用此函数之前进行一次code>。通过PHP脚本
->您的PHP脚本到底在哪里?我刚刚编辑了它,请看一看。我不愿意发布完整的脚本,因为它可能会影响以后的安全性,因为在某些概念中,这是一种“防泄漏”。@JohnKap它不返回字符串,而是返回字符*
。你应该记住curl是一个c-API。啊,我忘了!所以我就去;字符串result2=结果(字符);你看过说明书了吗?好吧,现在我明白了libc++abi.dylib:在创建URL时,以std::runtime\u类型的未捕获异常终止错误:URL使用错误/非法格式或缺少URL
<代码>标准::字符串url=”https://antrax.pw/asdojaOSJdu2bashDL/jaDHISJdhajKDHkas/sjhadLIUSD2bjhasjdn/ajHDIUj2nsADJhmashD/usercheck.php?hwid=“+hwid代码>如果(代码!=CURLE\u OK)抛出std::runtime\u error(curl\u easy\u strerror(代码)),它也抛出这个
@JohnKap我想这是因为您的URL需要SSL
(https)。您需要为此设置其他选项。这个例子可能有助于我将其添加到http_get函数中<代码>curl\u easy\u setopt(&curl,CURLOPT\u USE\u SSL,CURLUSESSL\u ALL)代码>,但我仍然会遇到同样的错误。@JohnKap-hmm。这对我很有用。你能用一个更简单的URL(比如http://www.google.com
)?