我正在除錯一個使用 GuzzleHttp 的 symfony 應用程式,但在嘗試了大約 8 小時后它仍然無法作業我無法使用 guzzle 連接到我的頁面。該應用程式與目標網站不在同一服務器/域上. 我嘗試了 100 種語法,但如果有人能告訴我哪里錯了,我仍然無法登錄,我將非常感激。
順便說一句,當我執行 var_dump 或 echo 時,我沒有任何錯誤,它只是給了我登錄頁面
Goutte\Client 是一個自定義類來處理請求,當我來到這個專案時就像這樣,所以我讓它這樣。
好的,經過多次嘗試,guzzle 的“auth”屬性似乎根本不起作用,因為我得到了正確的登錄頁面,但我什至沒有連接一次。
順便說一句,我希望你圣誕快樂,新年快樂。
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
$crawler = $client->request('POST', "URL", [ 'auth' => ['user', 'pass'] ]);
$crawler = $client->request('GET', "URL");
還有一個叫做 Client 類的方法
protected function doRequest($request)
{
$headers = array();
foreach ($request->getServer() as $key => $val) {
$key = strtolower(str_replace('_', '-', $key));
$contentHeaders = array('content-length' => true, 'content-md5' => true, 'content-type' => true);
if (0 === strpos($key, 'http-')) {
$headers[substr($key, 5)] = $val;
}
// CONTENT_* are not prefixed with HTTP_
elseif (isset($contentHeaders[$key])) {
$headers[$key] = $val;
}
}
$body = null;
if (!in_array($request->getMethod(), array('GET', 'HEAD'))) {
if (null !== $request->getContent()) {
$body = $request->getContent();
} else {
$body = $request->getParameters();
}
}
$this->getClient()->setDefaultOption('auth', $this->auth);
$requestOptions = array(
'body' => $body,
'cookies' => $this->getCookieJar()->allRawValues($request->getUri()),
'allow_redirects' => false,
);
if (!empty($headers)) {
$requestOptions['headers'] = $headers;
}
$guzzleRequest = $this->getClient()->createRequest(
$request->getMethod(),
$request->getUri(),
$requestOptions
);
foreach ($this->headers as $name => $value) {
$guzzleRequest->setHeader($name, $value);
}
if ('POST' == $request->getMethod() || 'PUT' == $request->getMethod()) {
$this->addPostFiles($guzzleRequest, $request->getFiles());
}
// Let BrowserKit handle redirects
try {
$response = $this->getClient()->send($guzzleRequest);
} catch (RequestException $e) {
$response = $e->getResponse();
if (null === $response) {
throw $e;
}
}
return $this->createResponse($response);
}
定義“auth”屬性的方法
public function setAuth($user, $password = '', $type = 'basic')
{
$this->auth = array($user, $password, $type);
return $this;
}
public function resetAuth()
{
$this->auth = null;
return $this;
}
提前感謝您的回答
uj5u.com熱心網友回復:
這部分在這里沒有什么意義,因為"URL"它似乎不是一個有效的 URL ......
而且上一個結果$client->request()也被下一個結果覆寫。
$crawler = $client->request('POST', "URL", [ 'auth' => ['user', 'pass'] ]);
$crawler = $client->request('GET', "URL");
在模擬瀏覽器登錄時,這通常遵循以下方案:
// define base URL
$base_url = 'https://www.example.com/';
// first load the form
$result = $client->request('GET', $base_url . 'login');
// do something with result, eg. parse the form nonce.
// then post the form
$result = $client->request('POST', $base_url . 'login', [
'auth' => ['insert username and', 'password here']
]);
// do something with result
uj5u.com熱心網友回復:
我建議檢查 Content-Type 標頭,因為在某些情況下 POST 資料不會被 symfony w\oa 有效內容型別識別。或許,這就是問題所在。看看服務器上的身份驗證代碼會很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408784.html
標籤:
上一篇:PHP-LDAP修改密碼
