curl的问题无法弄清楚

时间:2015-04-07 20:11:41

标签: php curl

我有卷曲问题

下面是代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.paypal.com/cgi-bin/webscr');
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $workString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Host: www.paypal.com"));
$output = curl_exec($ch);
curl_close($ch);

当我将payl从paypal更改为google.co.in时,我得到$ output的空白值。它的工作原理为什么paypal在我的服务器上表现得很有趣或者我错过了什么

1 个答案:

答案 0 :(得分:1)

您错过了回复:

Cache-Control: max-age=0, no-cache, no-store, must-revalidate
Connection: keep-alive, Transfer-Encoding
Content-Encoding: gzip
Content-Type: text/html
DC: dcg11-origin-www-2.paypal.com
Date: Thu, 09 Apr 2015 20:00:04 GMT
Location: https://www.paypal.com/home
Pragma: no-cache
Server: Apache
Set-Cookie: cwrClyrK4LoCV1fydGbAxiNL6iG=S9TU1gy_lJEUvQYrEjEdnxVIcMALl-A4QetP0un3pgYUjRvcqyV3PPcZfdmXw-Gndz2OpzOwWjVxel3EHUlf2UByp5YvehcKVr0j7Cc6bWGV7-rzs92RJPhyzkaHNvee0iTM4lSH1BqczOa0fALSl6SBiPsdCURen6MjyC9HRs2ntATq9CBbtaI6hJG2gPVJUanxZ3JTjxkkHcigCXmXJjjDyt8SSs0pQdlw8Bm2_NqGGcd7pOZW3H-AEaG; domain=.paypal.com; path=/; Secure; HttpOnly
KHcl0EuY7AKSMgfvHl7J5E7hPtK=ny66wHhnb7-YsQ5Khe83cIfgHbL2sOuqh4501OqRIsavyJB1_k9paRgZmyFdqxEqed9jGwPrvOpMgP8W; expires=Wed, 04-Apr-2035 20:00:04 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
cookie_check=yes; expires=Sun, 06-Apr-2025 20:00:04 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
consumer_display=USER_HOMEPAGE%3d0%26USER_TARGETPAGE%3d0%26USER_FILTER_CHOICE%3d0%26BALANCE_MODULE_STATE%3d1%26GIFT_BALANCE_MODULE_STATE%3d1%26LAST_SELECTED_ALIAS_ID%3d0%26SELLING_GROUP%3d1%26PAYMENT_AND_RISK_GROUP%3d1%26SHIPPING_GROUP%3d1%26HOME_VERSION%3d1%26MCE2_ELIGIBILITY%3d4294967295; expires=Sun, 06-Apr-2025 20:00:04 GMT; domain=.paypal.com; path=/; Secure; HttpOnly
Apache=10.16.0.148.1428609604137838; path=/; expires=Sat, 01-Apr-45 20:00:04 GMT
X-PP-SILOVER=name%3DLIVE9.WEB.1%26silo_version%3D880%26app%3Dappdispatcher%26TIME%3D1155147349; domain=.paypal.com; path=/; Secure; HttpOnly
X-PP-SILOVER=; Expires=Thu, 01 Jan 1970 00:00:01 GMT
Apache=10.16.0.11.1428609604131575; path=/; expires=Sat, 01-Apr-45 20:00:04 GMT
AKDC=dcg11-origin-www-2.paypal.com; expires=Thu, 09-Apr-2015 20:30:04 GMT; path=/; secure
Strict-Transport-Security: max-age=63072000
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN

您需要的两件事:

请注意回复标题:Location: https://www.paypal.com/home

网址返回了302重定向url_getinfo($ch,CURLINFO_HTTP_CODE)

$status = intval(curl_getinfo($ch,CURLINFO_HTTP_CODE));
if ($status > 299 && $status < 400){
  $location = curl_getinfo($ch,CURLINFO_REDIRECT_URL );
}

这是因为您没有设置CURLOPT_FOLLOWLOCATION

首先要尝试:


curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

上面你可能需要饼干罐。

CURLOPT_COOKIEJAR
CURLOPT_COOKIEFILE

您可能需要额外的Request Header键值,例如Accept:

$request = array();
$request[] = 'Host: www.paypal.com';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$request[] = 'Cookie: xxxx

curl_setopt($ch, CURLOPT_HTTPHEADER, $request);

如果有必要,您下次请求将转到重定向位置。

这些是您的Request标头键值:
注意:显示的cookie值已经过编辑且无效

Host: www.paypal.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Cookie: cwrClyrK4LoCV3fydGbAxiNL6iG=S9TU1gy_lJEUvQYrEjEdnxVIcMALl-A4QetP0un3pgYUjRvcqyV3PPcZfdmXw-Gndz2OpzOwWfVxel3EHUlf2UByp5YvehcKVr0j7Cc6bWGV7-rzs92RoaH1BqczOa0fALSl6SBiPsdCURen6MjyC9HRs2nta0digPVJUanxZ3JTjxkkHcigCXwXJjjDyt8SSs0pQdlw8Bm2_NqGGcd7pOZW3H-AEaG; KHcl0EuY7AKSMgfvHl7J5E7hPtK=ny66wHhnb7-YsQ5Khe83cIfgHbL2sOuqh4501OqRIsavyJB1_k9paRgZmyFdqxEqed9jGwPrvOpMgP8W; cookie_check=yes; consumer_display=USER_HOMEPAGE%3d0%26USER_TARGETPAGE%3d0%26USER_FILTER_CHOICE%3d0%26BALANCE_MODULE_STATE%3d1%26GIFT_BALANCE_MODULE_STATE%3d1%26LAST_SELECTED_ALIAS_ID%3d0%26SELLING_GROUP%3d1%26PAYMENT_AND_RISK_GROUP%3d1%26SHIPPING_GROUP%3d1%26HOME_VERSION%3d1%26MCE2_ELIGIBILITY%3d4294967295; Apache=10.16.0.11.1428609604131575; X-PP-SILOVER=name%3DLIVE9.WEB.1%26silo_version%3D880%26app%3Dappdispatcher%26TIME%3D1155147349; AKDC=dcg11-origin-www-2.paypal.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache