I'm debugging a symfony app that use GuzzleHttp but after like eight hours of trying something it still doesn't work I can't connect to my page using guzzle.The application isn't on the same server/domain as the target website. I tried like 100 syntax but I still can't login if someone can tell me where I'm wrong I would be really thankful.
By the way I don't have any error when I do a var_dump or an echo it just gives me the login page
Goutte\Client is a custom class to handle request it was like that when I come to the project so I let it like that.
Ok, so after a lot of tries it seems that the 'auth' property from guzzle isn't working at all because I get the right login page but I didn't even connect once.
By the way I hope you'll have a Merry Christmas and a Happy New Year.
use Goutte\Client;
use Symfony\Component\DomCrawler\Crawler;
$crawler = $client->request('POST', "URL", [ 'auth' => ['user', 'pass'] ]);
$crawler = $client->request('GET', "URL");
And there is the Client class method who's called
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);
}
Methods that define 'auth' propertie
public function setAuth($user, $password = '', $type = 'basic')
{
$this->auth = array($user, $password, $type);
return $this;
}
public function resetAuth()
{
$this->auth = null;
return $this;
}
Thank you for your answers by advance
CodePudding user response:
This part here makes little sense, because "URL" does not appear to be a valid URL ...
also the previous result of $client->request() is being overwritten by the next one result.
$crawler = $client->request('POST', "URL", [ 'auth' => ['user', 'pass'] ]);
$crawler = $client->request('GET', "URL");
When emulating a browser-login, this may commonly follow this scheme:
// 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
CodePudding user response:
I would suggest checking the Content-Type header as in some cases POST data won't be recognized by symfony w\o a valid content type. Perhaps, this is the problem. Would be good to look at the auth code on the server.
