卷曲方法 GET 不起作用

发布于 2024-12-05 06:04:40 字数 843 浏览 0 评论 0原文

我一直在尝试让curl 从表单发送GET 方法。我已经构建了将所有表单数据连接在一起的 URL。如果我直接在地址栏中输入 get 请求,它可以工作,但不能与curl一起工作。如果我回显 $data,我实际上会收到一条错误,指出“未找到对象!”我尝试显示curl_error()内容,但它返回空。非常感谢大家的帮助。

这就是我到目前为止所得到的:

$url = complete url of get request that works when directly placed into adress bar
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data;

谢谢大家帮助我。我尝试了下面的代码,它确实说“成功”。当我回显 $data 时,我瞬间看到“成功”,然后我得到“未找到对象!”错误。

$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
    echo "Success";
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec ($ch);
    curl_close ($ch);

}
else {
    echo "Could not initialize curl session";
}

我可能会收到错误,因为我将 GET 转发到的链接会将其转发到另一个链接来处理数据。

I have been trying to get curl to send a GET method from a form. I have constructed the URL with all the form data concatenated. If I type in the get request directly into the address bar it works but will not work with the curl. If i echo the $data I will actually receive an error stating "Object not found!" I tried displaying curl_error() contents and it return empty. Thank you very much everyone for your help.

This is what i have so far:

$url = complete url of get request that works when directly placed into adress bar
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec ($ch);
curl_close ($ch);
echo $data;

Thank you for helping me everyone. I tried the following code and it does say "sucess". When I echo $data, I see a "sucess" for a split second and then I get the "Object not found!" error.

$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
    echo "Success";
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec ($ch);
    curl_close ($ch);

}
else {
    echo "Could not initialize curl session";
}

I may be getting errors because the the link I forward the GET to will forward it to another link to process the data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风筝在阴天搁浅。 2024-12-12 06:04:40

尝试添加

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;

这应该强制它以字符串形式返回数据而不是输出它。

有关更多详细信息,请参阅文档

Try adding the

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;

This should force it to return the data as a string instead of outputting it.

See the documentation for more details.

错々过的事 2024-12-12 06:04:40

尝试使用 CURLOPT_RETURNTRANSFER 选项:

   $ch = curl_init();
    $url = complete url of get request that works when directly placed into adress bar
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $data = curl_exec ($ch);
    curl_close ($ch);
    echo $data;

Try using the CURLOPT_RETURNTRANSFER option:

   $ch = curl_init();
    $url = complete url of get request that works when directly placed into adress bar
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $data = curl_exec ($ch);
    curl_close ($ch);
    echo $data;
嘦怹 2024-12-12 06:04:40

我相信您缺少一个用于初始化会话的curl_init。

使用建议的代码进行编辑

尝试复制并粘贴以下代码:

$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
    echo "Success";
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec ($ch);
    curl_close ($ch);
    echo $data;
}
else {
    echo "Could not initialize curl session";
}

并让我们知道curl 会话是否已成功初始化。

I believe you are missing a curl_init to initialize the session.

Edit with suggested code

Try copying and pasting the below code:

$url = complete url of get request that works when directly placed into adress bar
$ch = curl_init($url);
if ($ch) {
    echo "Success";
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $data = curl_exec ($ch);
    curl_close ($ch);
    echo $data;
}
else {
    echo "Could not initialize curl session";
}

And let us know if the curl session is initialized successfully.

风吹短裙飘 2024-12-12 06:04:40

我遇到了同样的问题并解决如下:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS , null );
    curl_setopt($ch, CURLOPT_POST , 0);
    curl_setopt($ch, CURLOPT_HTTPGET , TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT , 4);
    curl_setopt($ch, CURLOPT_USERAGENT , "" );
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_URL , $url );
    $rgxrData = curl_exec($ch);  // rgxrData stands for response get xml raw Data
    echo "Get curl_errno = " ;
    echo curl_errno($ch) ;

    if (curl_errno($ch)) {
            $error_message = curl_error($ch);
            $error_no = curl_errno($ch);

            echo "error_message: " . $error_message . "<br>";
            echo "error_no: " . $error_no . "<br>";
    }
    curl_close($ch);

    return $rgxrData;

遵循从发布到获取重置的正确顺序非常重要。即首先将 postfields 设置为 null,然后将 post 设置为 0,然后将 httpget 设置为 true。一旦你设置了这个,那么你就可以放心,服务器将开始准备下一个请求,就像 get 请求一样,否则不会。此后,设置选项的其余部分和 url(以及所有参数)。这将确保正确发出并执行获取请求。

希望这有帮助...

I faced the same issue and resolved it as follows:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POSTFIELDS , null );
    curl_setopt($ch, CURLOPT_POST , 0);
    curl_setopt($ch, CURLOPT_HTTPGET , TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE);
    curl_setopt($ch, CURLOPT_TIMEOUT , 4);
    curl_setopt($ch, CURLOPT_USERAGENT , "" );
    curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
    curl_setopt($ch, CURLOPT_URL , $url );
    $rgxrData = curl_exec($ch);  // rgxrData stands for response get xml raw Data
    echo "Get curl_errno = " ;
    echo curl_errno($ch) ;

    if (curl_errno($ch)) {
            $error_message = curl_error($ch);
            $error_no = curl_errno($ch);

            echo "error_message: " . $error_message . "<br>";
            echo "error_no: " . $error_no . "<br>";
    }
    curl_close($ch);

    return $rgxrData;

It is important that you follow the correct order for reset from post to get. ie first set the postfields to null, then post to 0 and then httpget to true. once you have set this then you can be assured that the server will start preparing the next request like a get request else not. Thereafter, set the rest of the option and the url(along with all the parameters). This will ensure the get request is properly made and executed.

Hope this helps...

乱了心跳 2024-12-12 06:04:40

请尝试 http_build_query() 函数来创建查询字符串我认为这会有所帮助,它在我的情况下有效

Please try http_build_query() function for create query string i think this will help , it is work in my case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文