简体   繁体   English

PHP cURL没有错误,但不起作用

[英]PHP cURL No Error but not working

I've been banging my head on this for a while now and I'm sorta fed up. 我已经为此努力了一段时间,我有点受够了。 I'm not a PHP programmer so I might be missing something that is not immediately obvious to my Python infused brain. 我不是PHP程序员,所以我可能会丢失一些对我注入Python的大脑不立即可见的东西。

So here's the context. 这就是上下文。 I need to write a script to login automatically into a web interface and run a search, and well, absolutely everything I've tried to do seems to fail miserably. 我需要编写一个脚本以自动登录到Web界面并运行搜索,而且,绝对地,我尝试做的所有事情似乎都失败了。

Here's my code: 这是我的代码:

<?php
echo 'start';
// INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD

curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, '/var/tmp/cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_FRESH_CONNECT, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

if($store == False){
        echo " store false ";
        echo curl_error($ch);
} else {
        echo " store true ";
        echo $store;
}

// SET FILE TO DOWNLOAD
echo ' second_request ';
curl_setopt($ch, CURLOPT_URL, 'https://W0110DcIpsOcsRpt02/si_ocs_gui/FWOCS1_BL_SUBSCRIBERS_list.php');
curl_setopt ($ch, CURLOPT_POST, 1);
#curl_setopt ($ch, CURLOPT_POSTFIELDS, 'value_ACCESS_NO_4=15142351150');
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'q=\(SUBSCRIBER_ID~equals~1545303\)');
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
if(!$store == False){
        $content = curl_exec ($ch);
        echo $content;
}
// CLOSE CURL

curl_close($ch);

echo ' done';
?>

The behaviour of this is as follows. 其行为如下。 I run the script, and it fails at the first cURL request (the login) and returns no errors. 我运行了脚本,但是在第一个cURL请求(登录)时它失败了,并且没有返回错误。 I have modified this line here: 我在这里修改了这一行:

curl_setopt ($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&btnSubmit=Login');

by removing the btnSubmit=Login part and I get the login page displayed with the filled in username and password fields. 通过删除btnSubmit=Login部分,我得到显示的登录页面,其中包含用户名和密码字段。 I can press the login button and it will work. 我可以按登录按钮,它将起作用。 However, it seems that my lack of web development is biting me here and I have no idea how that button works. 但是,似乎我缺乏网络开发正使我难受,而且我不知道该按钮的工作原理。 So, here's the code inspected with firebug: 因此,这是使用firebug检查的代码:

<a class="rnr-button main" href="#" onclick="document.forms[0].submit();return false;">Submit</a>

I also went directly in the PHP code on the web server and found the button corresponding to it: 我也直接进入了Web服务器上的PHP代码,并找到了与之对应的按钮:

if ((@$_POST["btnSubmit"] == "Login" || $adSubmit) && $logacc)

Hence why I was trying the btnSubmit=Login . 因此,为什么我要尝试btnSubmit=Login

So my question is pretty simple: what am I doing wrong and how can I get the results I need? 所以我的问题很简单:我做错了什么,如何获得所需的结果?

I have got the same problem and find the solution for it. 我遇到了同样的问题,并找到了解决方案。

Code

CURLOPT_RETURNTRANSFER => false CURLOPT_RETURNTRANSFER =>否

Use this to solve your problem. 使用它来解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM