我知道類似的問題已被問到,但我認爲他們有點不同或他們還沒有解決我的問題。首先,我幾個星期前纔開始學習PHP/curl和網站,所以我確信我犯了很多錯誤。出於這個原因,我會問你的理解。Php捲曲腳本工作本地主機(xampp),但不是當我上傳到託管
好了,感謝StackOverflow的我已經能夠「寫」的劇本,其功能是:
- 登錄www.example.com/login.php
- 上WWW「填表」 ?.example.com的/ submit.php H =測試
的代碼如下:
code.php
<?php
error_reporting (E_ALL | E_STRICT);
set_time_limit(0);
function curl_login($url,$data,$proxy,$proxystatus){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, $url);
ob_start(); // prevent any output
$temp=curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
$fp = fopen("cookie.txt", "w");
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start(); // prevent any output
return curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
}
$test1 = curl_login('http://www.example.com/login.php','username=itsme&password=abcdedg&login=Login','','off');
function curl_submit($url,$data,$proxy,$proxystatus){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_URL, $url);
ob_start(); // prevent any output
$temp=curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
$fp = fopen("cookie.txt", "w");
fclose($fp);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_TIMEOUT, 130);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start(); // prevent any output
return curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
}
$test2 = curl_submit('http://www.example.com/submit.php?h=test','url=www.heythere.com&username=xyz&submit=submit','','off');
echo $test2;
?>
奇怪的(對我來說)是,當我在XAMPP運行一切都運行完美,但是當我從我的主機運行(在這個例子是http://www.example.com/code.php)是不能夠登錄並頭是:
HTTP/1.1 301永久移動日期:星期六,2015年8月15日十點50分26秒GMT 服務器:Apache截止日期:星期一,1997 05:00:00 GMT的Cache-Control 7月26日: 沒有-store,no-cache,must-revalidate,post-check = 0,pre-check = 0 Pragma: no-cache Set-Cookie:HEY = 1342453645645645645; path =/Last-Modified: Sat,15 Aug 2015 10:50:26 GMT位置: http://www.example.com/login.php不同:Accept-Encoding,User-Agent Content-Length:0 Content-Type:text/html HTTP/1.1 200 OK日期:星期六, 2015年8月15日10:50:26 GMT服務器:Apache過期時間:星期四,1981年11月19日 08:52:00 GMT Cache-Control:no-store,no-cache,must-revalidate, post- check-0,pre-check = 0 Pragma:no-cache Last-Modified:Sat,15 Aug 2015 10:50:26 GMT Vary:Accept-Encoding,User-Agent Transfer-Encoding: chunked Content-Type:text/html
我希望一切都很清楚,你沒有看到這麼多的錯誤!
您確定curl安裝在您的託管服務器上嗎?檢查[這篇文章] [1] [1]:http://stackoverflow.com/questions/13433946/how-to-check-if-curl-is-enabled-or-disabled – HarveyEV
是的,它被安裝,我已經檢查過了!從應用程序路徑:curl 7.19.7 – karl
您的網站的cuz在運行您的代碼之前已被重定向。它可以是非www的cuz。或其他任何東西。請控制您的重定向。您可以使用該鏈接:http://www.redirect-checker.org/ –