Php curl方法不起作用

Php curl方法不起作用,php,curl,Php,Curl,我一直在尝试让curl从表单发送get方法。我已经用所有的表单数据连接起来构建了URL。如果我直接在地址栏中输入get请求,它可以工作,但不能使用curl。如果我回显$data,我实际上会收到一个错误,说明“找不到对象!”我尝试显示curl_error()内容,结果返回空。非常感谢大家的帮助 这就是我到目前为止所做的: $url = complete url of get request that works when directly placed into adress bar curl_s

我一直在尝试让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;

谢谢大家帮助我。我尝试了下面的代码,它确实显示“success”。当我回显$data时,我会在一瞬间看到一个“success”,然后出现“objectnotfound!”错误

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

尝试添加

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
这将迫使它以字符串形式返回数据,而不是输出数据


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

尝试使用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;

我认为您缺少初始化会话的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会话是否成功初始化。

我遇到了同样的问题,并按如下方式解决了它:

    $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;
$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_超时,4);
curl_setopt($ch,CURLOPT_USERAGENT,”);
curl_setopt($ch,CURLOPT_VERBOSE,TRUE);
curl_setopt($ch,CURLOPT_URL,$URL);
$rgxrData=curl_exec($ch);//rgxrData代表响应获取xml原始数据
echo“Get curl_errno=”;
回声旋度(ch);
if(旋度误差($ch)){
$error\u message=curl\u error($ch);
$error\u no=curl\u errno($ch);
回显“错误消息:.$error\u message.”
; 回显“错误号:.$error\u no.”
; } 卷曲关闭($ch); 返回$rgxrData;
请务必按照正确的顺序从post重置以获取。ie首先将postfields设置为null,然后将post设置为0,然后将httpget设置为true。一旦您设置了这个,您就可以确信服务器将开始准备下一个请求,就像get请求一样,否则就不会。然后,设置选项的其余部分和url(以及所有参数)。这将确保正确发出并执行get请求


希望这有帮助…

请尝试使用http\u build\u query()函数创建查询字符串,我认为这会有所帮助,在我的情况下这是可行的

您是否设置了
CURLOPT\u RETURNTRANSFER
?否则,您不应该期望从
curl\u exec()
获取数据作为返回值……感谢您的快速回复。我确实尝试添加了那行代码,但仍然收到了“Object not found!”。在您发布的所有其他代码之上是否有一行curl_init?是“Object not found!”来自您的代码的某个地方,还是来自echo$数据;线路?有没有办法找出错误的来源?谢谢。嗯,我真的很抱歉它仍然不起作用。我感谢大家的帮助$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$URL);curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1)$数据=curl_exec($ch);卷曲关闭($ch);回波数据;如果上述方法不起作用,请尝试访问另一个url,看看会发生什么。
    $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;