我已经阅读了几个关于这个问题的帖子,但我根本无法破解这个问题。
我正在尝试让PHP Curl模拟此Web浏览器请求: https://www.endomondo.com/embed/user/workouts/?id=747232&measure=0&width=450&height=400&page=0&language=DA
(我已在此请求之前删除了所有Cookie)
当我从浏览器访问URL时,我得到一个状态代码200,一切都很好。但是,当我使用PHP Curl时,我得到状态代码403,这表示服务器由于某种原因拒绝了我的请求。
这是我的PHP代码:
$curl_cookie = tempnam("/tmp", "tmpcookie.txt");
$curl_url = 'https://www.endomondo.com/embed/user/workouts/?' . http_build_query(Array('id' => 747234 , 'measure' => 0 , 'width' => 450 , 'height' => 400 , 'page' => 0, 'language' => 'DA'));
$curl_header = array(
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding: gzip, deflate, br",
"Accept-Language: en,da;q=0.7,en-US;q=0.3",
"Cache-Control: max-age=0",
"Connection: keep-alive",
"DNT: 1",
"Upgrade-Insecure-Requests: 1",
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"
);
$curl_options = array(
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_URL => $curl_url,
CURLOPT_HTTPHEADER => $curl_header,
CURLOPT_COOKIESESSION => true,
CURLOPT_COOKIEFILE => "",
CURLOPT_COOKIEJAR => $curl_cookie,
CURLOPT_VERBOSE => true
);
$ch = curl_init();
curl_setopt_array( $ch, $curl_options );
$ch_content = curl_exec($ch);
$ch_err = curl_errno($ch);
$ch_errmsg = curl_error($ch);
$ch_header = curl_getinfo($ch);
curl_close($ch);
echo "ERROR_NO : " .$ch_err;
echo "ERROR_MSG : " .$ch_errmsg;
print_r ($ch_header);
echo $ch_content;
我已将Web请求标头与PHP Curl请求标头进行了比较。它们如下:
来自网络请求的HTTP请求标头(从Firefox调试模式复制):
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en,da;q=0.7,en-US;q=0.3
Cache-Control: max-age=0
Connection: keep-alive
DNT: 1
Host: www.endomondo.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
来自PHP Curl请求的HTTP标头:
GET /embed/user/workouts/?id=747232&measure=0&width=450&height=400&page=0&language=DA HTTP/1.1
Host: www.endomondo.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en,da;q=0.7,en-US;q=0.3
Cache-Control: max-age=0
Connection: keep-alive
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
为什么Endomondo拒绝我的请求?它可能会根据我的服务器的IP地址或类似的东西来阻止请求吗?
答案 0 :(得分:0)
是的,必须是某种IP块(也许是区域锁定?)。我从这里得到200。顺便说一句,因为你发送标题Accept-Encoding: gzip, deflate, br
,服务器选择gzip压缩输出,你没有提供解码它的代码,因此我的终端中得到了难以理解的二进制数据,
hanshenrik@Deb9DEtestX:~/phptests3$ php foo4.php
ERROR_NO : 0ERROR_MSG : Array
(
[url] => https://www.endomondo.com/embed/user/workouts/?id=747234&measure=0&width=450&height=400&page=0&language=DA
[content_type] => text/html;charset=UTF-8
[http_code] => 200
[header_size] => 1121
[request_size] => 454
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.362421
[namelookup_time] => 0.033952
[connect_time] => 0.081217
[pretransfer_time] => 0.162576
[size_upload] => 0
[size_download] => 1977
[speed_download] => 5461
[speed_upload] => 0
[download_content_length] => 1977
[upload_content_length] => -1
[starttransfer_time] => 0.362295
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 13.33.44.67
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 192.168.1.69
[local_port] => 40324
[request_header] => GET /embed/user/workouts/?id=747234&measure=0&width=450&height=400&page=0&language=DA HTTP/1.1
Host: www.endomondo.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en,da;q=0.7,en-US;q=0.3
Cache-Control: max-age=0
Connection: keep-alive
DNT: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
)
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 1977
Connection: keep-alive
Cache-Control: no-cache, max-age=0, must-revalidate, no-store
Content-Encoding: gzip
Content-Language: en
Date: Wed, 02 May 2018 15:21:56 GMT
P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
Pragma: no-cache
Server: Apache-Coyote/1.1
Set-Cookie: CSRF_TOKEN=6av0q6psn49etvdt3d4iqvj2uc; Path=/; Secure
Set-Cookie: JSESSIONID=34366B7693B2AB99E53203712CF4E96E; Domain=www.endomondo.com; Path=/; HttpOnly
Set-Cookie: AWSELB=13FDC17D1C2B68745BCB41EBB6BDDC3DB8274D93D632CEC9302FDAE832678AB6A3EFA1E6D580B2D673C8844CEA83A618537653BF381452F4DA3B7D27994EE9DB3B34D0802A11DD898C909C58E6B350BCF9DB0F4312;PATH=/;DOMAIN=www.endomondo.com;HTTPONLY
Strict-Transport-Security: max-age=15768000
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Endo-ID: NOT_SET
X-UA-Compatible: IE=9
X-XSS-Protection: 1; mode=block
X-Cache: Miss from cloudfront
Via: 1.1 123c1b8455da8ecd4abd0715a42e881e.cloudfront.net (CloudFront)
X-Amz-Cf-Id: QDjQfo8-aHVuRjup3_Ap9XSDDzsNVdyOR1zFNRd8Aipvvz5AesT3aQ==
�ZYs��}���R�J�uk��b�c�jxI ��m����珥Y
����LD
-�>������[�?���i�NY�u�ިҹ�Rt�a&\�aj���>t(X�0T��ƶ���Ի4E[7
�^�
k���&3�3��/��������,�X�o�����xQ�B7PQ���4娞Y����]H�o�ST��A�uO�]�l��.�%�W(\�h]d-vm]�Uj��G�K{U��������M�hJ;�h�b��0h��F�F^�)fߍc{ <<capped binary gibberish>>
但是,如果我剪掉垃圾,并制作代码
$ch = curl_init ();
curl_setopt_array ( $ch, array (
CURLOPT_URL => 'https://www.endomondo.com/embed/user/workouts/?' . http_build_query ( Array (
'id' => 747234,
'measure' => 0,
'width' => 450,
'height' => 400,
'page' => 0,
'language' => 'DA'
) ),
CURLOPT_VERBOSE => 1,
CURLOPT_ENCODING => ''
) );
curl_exec($ch);
,我明白了:
hanshenrik@Deb9DEtestX:~/phptests3$ php foo4.php
* Trying 13.33.44.62...
* TCP_NODELAY set
* Connected to www.endomondo.com (13.33.44.62) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=DK; ST=Copenhagen; L=Copenhagen; O=ENDOMONDO ApS; CN=*.endomondo.com
* start date: Jun 20 00:00:00 2016 GMT
* expire date: Aug 24 12:00:00 2018 GMT
* subjectAltName: host "www.endomondo.com" matched cert's "*.endomondo.com"
* issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
* SSL certificate verify ok.
> GET /embed/user/workouts/?id=747234&measure=0&width=450&height=400&page=0&language=DA HTTP/1.1
Host: www.endomondo.com
Accept: */*
Accept-Encoding: deflate, gzip
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=UTF-8
< Content-Length: 1979
< Connection: keep-alive
< Cache-Control: no-cache, max-age=0, must-revalidate, no-store
< Content-Encoding: gzip
< Content-Language: en
< Date: Wed, 02 May 2018 15:25:23 GMT
< P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"
< Pragma: no-cache
< Server: Apache-Coyote/1.1
< Set-Cookie: CSRF_TOKEN=4695uqd37dv2j2l54jh9a2rn0d; Path=/; Secure
< Set-Cookie: JSESSIONID=13898023D4D521726B524285E5B72C1F; Domain=www.endomondo.com; Path=/; HttpOnly
< Set-Cookie: AWSELB=13FDC17D1C2B68745BCB41EBB6BDDC3DB8274D93D615A7D975F2F390D054A556F773DC326FE5ED00DBCF12BF71655D1BB4061D616B1452F4DA3B7D27994EE9DB3B34D0802A11DD898C909C58E6B350BCF9DB0F4312;PATH=/;DOMAIN=www.endomondo.com;HTTPONLY
< Strict-Transport-Security: max-age=15768000
< Vary: Accept-Encoding
< X-Content-Type-Options: nosniff
< X-Endo-ID: NOT_SET
< X-UA-Compatible: IE=9
< X-XSS-Protection: 1; mode=block
< X-Cache: Miss from cloudfront
< Via: 1.1 f9fdb36a35b271b2043714f4c4655495.cloudfront.net (CloudFront)
< X-Amz-Cf-Id: chGCj7ws8YYSOYMQyEQ4OeQ1sitYnu1ffomtAs0ve65-ky-wnE2nmA==
<
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="uk">
<head>
<title>Endomondo Workout Overview</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<link rel="SHORTCUT ICON" href="http://www.endomondo.com/favicon.ico" />
<link rel="stylesheet" type="text/css" href="../../../css/reset-fbea64300020d6b01efc0eb9652c4aff.css" />
<link rel="stylesheet" type="text/css" href="../../../css/AbstractWidgetPage-753381f932c1912e802955160f06b654.css" />
<link rel="stylesheet" type="text/css" href="../../../css/WorkoutListWidgetPage-50c2bd86823816770781e894496dc3c0.css" />
</head>
<body>
<div style="width:450px;height:400px;">
<div class="header"><a href="http://www.endomondo.com" target="_blank"></a></div>
<div class="workoutListPanel">
<div class="info"><a href="../../../profile/747234" target="_blank"><span>Kasper Vork Steffensen</span></a></div>
<table>
<tr class="odd">
<th class="date">Date</th>
<th class="sport">Sport</th>
<th class="distance">Distance</th>
<th class="duration">Duration</th>
</tr>
<tr class="even">
<td class="date"><a href="../../../workouts/1111903096/747234" target="_blank"><span>01-05-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1111903096/747234" target="_blank"><span>Vandring</span></a></td>
<td class="distance"><a href="../../../workouts/1111903096/747234" target="_blank"><span>5.97 km</span></a></td>
<td class="duration"><a href="../../../workouts/1111903096/747234" target="_blank"><span>1t:11m:40s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1110336783/747234" target="_blank"><span>29-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1110336783/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1110336783/747234" target="_blank"><span>2.23 km</span></a></td>
<td class="duration"><a href="../../../workouts/1110336783/747234" target="_blank"><span>13m:37s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1107934332/747234" target="_blank"><span>25-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1107934332/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1107934332/747234" target="_blank"><span>2.16 km</span></a></td>
<td class="duration"><a href="../../../workouts/1107934332/747234" target="_blank"><span>12m:52s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1103220617/747234" target="_blank"><span>18-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1103220617/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1103220617/747234" target="_blank"><span>1.73 km</span></a></td>
<td class="duration"><a href="../../../workouts/1103220617/747234" target="_blank"><span>10m:41s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1101464688/747234" target="_blank"><span>15-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1101464688/747234" target="_blank"><span>Cykling (sport)</span></a></td>
<td class="distance"><a href="../../../workouts/1101464688/747234" target="_blank"><span>18.87 km</span></a></td>
<td class="duration"><a href="../../../workouts/1101464688/747234" target="_blank"><span>57m:12s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1101464664/747234" target="_blank"><span>15-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1101464664/747234" target="_blank"><span>Cykling (sport)</span></a></td>
<td class="distance"><a href="../../../workouts/1101464664/747234" target="_blank"><span>3.21 km</span></a></td>
<td class="duration"><a href="../../../workouts/1101464664/747234" target="_blank"><span>9m:20s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1097658381/747234" target="_blank"><span>09-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1097658381/747234" target="_blank"><span>Vandring</span></a></td>
<td class="distance"><a href="../../../workouts/1097658381/747234" target="_blank"><span>2.91 km</span></a></td>
<td class="duration"><a href="../../../workouts/1097658381/747234" target="_blank"><span>35m:23s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1096931946/747234" target="_blank"><span>08-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1096931946/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1096931946/747234" target="_blank"><span>4.04 km</span></a></td>
<td class="duration"><a href="../../../workouts/1096931946/747234" target="_blank"><span>24m:08s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1096886974/747234" target="_blank"><span>08-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1096886974/747234" target="_blank"><span>Cykling (sport)</span></a></td>
<td class="distance"><a href="../../../workouts/1096886974/747234" target="_blank"><span>1.32 km</span></a></td>
<td class="duration"><a href="../../../workouts/1096886974/747234" target="_blank"><span>3m:54s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1096886972/747234" target="_blank"><span>08-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1096886972/747234" target="_blank"><span>Cykling (sport)</span></a></td>
<td class="distance"><a href="../../../workouts/1096886972/747234" target="_blank"><span>1.15 km</span></a></td>
<td class="duration"><a href="../../../workouts/1096886972/747234" target="_blank"><span>3m:43s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1094415381/747234" target="_blank"><span>04-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1094415381/747234" target="_blank"><span>Vandring</span></a></td>
<td class="distance"><a href="../../../workouts/1094415381/747234" target="_blank"><span>0.21 km</span></a></td>
<td class="duration"><a href="../../../workouts/1094415381/747234" target="_blank"><span>2m:08s</span></a></td>
</tr><tr class="odd">
<td class="date"><a href="../../../workouts/1094415380/747234" target="_blank"><span>04-04-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1094415380/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1094415380/747234" target="_blank"><span>4.01 km</span></a></td>
<td class="duration"><a href="../../../workouts/1094415380/747234" target="_blank"><span>23m:26s</span></a></td>
</tr><tr class="even">
<td class="date"><a href="../../../workouts/1092251072/747234" target="_blank"><span>31-03-2018</span></a></td>
<td class="sport"><a href="../../../workouts/1092251072/747234" target="_blank"><span>Løb</span></a></td>
<td class="distance"><a href="../../../workouts/1092251072/747234" target="_blank"><span>5.18 km</span></a></td>
<td class="duration"><a href="../../../workouts/1092251072/747234" target="_blank"><span>29m:55s</span></a></td>
</tr>
</table>
<div class="pagingNavigator">
<a class="increment prev" href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14NI1BBgJKQ9Ro3VDtDw2SQEqGMLqHeh5JBUFvzUzoPA9BX*MJ1A3yl76yiZlD4Ef0" title="Go to previous page">«</a>
<span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14NI1BBgJKQ9Ro3VDtDw2SQEqGMLqHeh5JBUFvzUzoPA9BX*MJ1A3yl76yiZlD4Ef0" class="selected" title="Go to page 1"><span>1</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy16cRb3wjujcju*3BZBwHjvQXmkI0we1*d6qmE-uhnWVewDm0r4qn3wNvPVCof4JA-o" title="Go to page 2"><span>2</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14zvvC833wStWAD6GH2W*W1MeKZ2UjoscWFZcdkXomFKfWsVZtvDCKnjKr*W8zmCoA" title="Go to page 3"><span>3</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy141UjG5jfsnjtklhKX9zWjFnWJufJ96IVQSwpkazUWBxtux4-ZoUjjNkw--PlD9ATM" title="Go to page 4"><span>4</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14vFxO-XX-Pr3OLVZlbUhEC1S0HsIWjhwdqazUL01pQ*3OuCkVNdyAolz3KNjDu7Zc" title="Go to page 5"><span>5</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy16d0OTPKNNys-KbPfxHWVowxMC0fss0aJfG-8Oae*BN-IGwLrAwFKLiICbNRNuN6Ow" title="Go to page 6"><span>6</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy16uLOl4HNpfretBlntOKWaMGac94op3gSvENFjKzq*VloUIRuZq99dDx7Ajjo3GQCU" title="Go to page 7"><span>7</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14C8FPsR6Z8gTutZSR9V39cUjfe-vvzXg7WAV2FBdLkLSFTyYjhSbCqyKHjvYPeXMg" title="Go to page 8"><span>8</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy14wjVY5*ug2Z192nyGp8WylvKcCvjxMIw2MA6fYWG3gn*LVWaS7cn6uN8xOojypAr8" title="Go to page 9"><span>9</span></a>
</span><span>
<a href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy16cKWKtIRKX4tYvYxj9jE2L5R*CFhnCbGHbT5-YXYbK46Od95BK*0LQa21qSSNUVjg" title="Go to page 10"><span>10</span></a>
</span>
<a class="increment next" href="../../../embed/user/workouts?x=RkMJLyf-*GcaLImo2rxKtq5GI8K0ZJJAceVDX5pvy16cRb3wjujcju*3BZBwHjvQXmkI0we1*d6qmE-uhnWVewDm0r4qn3wNvPVCof4JA-o" title="Go to next page">»</a>
</div>
</div>
<div class="footer"><span>Join us on <a href="http://www.endomondo.com" target="_blank">www.endomondo.com</a></span></div>
</div>
</body>
</html>* Connection #0 to host www.endomondo.com left intact
(ps,请注意我如何使用CURLOPT_ENCODING,使curl提供gzip压缩,并自动为我解压缩),