我正在尝试在 Groupon Merchant 中实现兑换优惠券的方式。
客户服务告诉我没有 API,所以我试图通过 CURL 请求来实现。
我可以提取 CSRF,但是无论我做什么,在发送数据时,我总是收到 403 错误。
如果我发送 CURLOPT_USERAGENT 我也会收到错误 403。
我不知道这是否与为标题发送一系列 cookie 有关。
所以我不知道我是做错了什么还是做不到。
有没有人知道如何做到这一点,目前我只是在尝试登录。
我已将私人数据更改为 xxxxx。
$url = "https://www.groupon.es/merchant/center/login"; $username = "xxxxx"; $password = "xxxxx"; $sh = curl_share_init(); curl_share_setopt($sh, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); //$cookie = 'C:/Temp/cookies.txt'; //Initialize Handle $handle1 = curl_init(); curl_setopt($handle1, CURLOPT_SHARE, $sh); curl_setopt($handle1, CURLOPT_URL, $url); //curl_setopt($handle1, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); curl_setopt($handle1, CURLOPT_RETURNTRANSFER, true); // get headers too with this line curl_setopt($handle1, CURLOPT_HEADER, 1); curl_setopt($handle1, CURLOPT_HEADERFUNCTION,'header_callback'); //curl_setopt($handle1, CURLOPT_COOKIEJAR, $cookie); //curl_setopt($handle1, CURLOPT_COOKIEFILE, $cookie); curl_setopt($handle1, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($handle1, CURLOPT_SSL_VERIFYPEER, 0); //Execute Request $output = curl_exec ( $handle1 ); var_dump($cookies); print($output); exit(); libxml_use_internal_errors(true); $dom = new DomDocument(); $dom->loadHTML($output); libxml_use_internal_errors(false); $tokens = $dom->getElementsByTagName("input"); if(!$tokens){ throw new Exception("Error"); }else{ echo ("Tokens: ". $tokens->length . "<br>"); } for ($i = 0; $i < $tokens->length; $i++) { $meta = $tokens->item($i); if($meta->getAttribute('name') == '_csrf'){ $t = $meta->getAttribute('value'); echo "Not Found</ br>". $t; } } $params = array( "redirect_uri" => "/merchant/center?mid=xxxxx", "email" => $username, "password" => $password, '_csrf' => $t, "mid" => "xxxxx" ); // get headers too with this line $headers = array( ':authority: www.groupon.es', ':method: POST', ':path: /merchant/center/login', ':scheme: https', 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'accept-encoding: gzip, deflate, sdch, br', 'accept-language: es-ES,es;q=0.8,en;q=0.6', 'cache-control: max-age=0', 'content-length: 180', 'content-type: application/x-www-form-urlencoded', 'origin: https://www.groupon.es', 'referer: https://www.groupon.es/merchant/center/login?redirect_uri=%2Fmerchant%2Fcenter%3Fmid%3Dxxxxx', 'sec-ch-ua: "Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"', 'sec-ch-ua-mobile: ?0', 'sec-ch-ua-platform: "Windows"', 'sec-fetch-dest: document', 'sec-fetch-mode: navigate', 'sec-fetch-site: same-origin', 'sec-fetch-user: ?1', 'upgrade-insecure-requests: 1', 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36', ); $handle2 = curl_init(); // curl_setopt($handle2, CURLOPT_SHARE, $sh); curl_setopt($handle2, CURLOPT_URL, $url); //curl_setopt($handle2, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36'); curl_setopt($handle2, CURLOPT_RETURNTRANSFER, true); curl_setopt($handle2, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); //curl_setopt($handle2, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($handle2, CURLOPT_HEADER, 1); curl_setopt($handle2, CURLOPT_HTTPHEADER, $headers); // curl_setopt($handle2, CURLOPT_COOKIEJAR, $cookie); // curl_setopt($handle2, CURLOPT_COOKIEFILE, $cookie); curl_setopt($handle2, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($handle2, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($handle2, CURLOPT_POST, TRUE); curl_setopt($handle2, CURLOPT_FOLLOWLOCATION, true); curl_setopt($handle2, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($handle2, CURLOPT_VERBOSE, true); $output = curl_exec ( $handle2 ); curl_share_close($sh); curl_close ( $handle1 ); curl_close ( $handle2 );
谢谢你!