Guzzle get request - php

I am trying to figure out how to download a file from a form that's on a website that requires login.
I can login with no issues using POST and the below code:
$client = new \GuzzleHttp\Client();
$response = $client->post('http://websitename.com/account/login.php', array(
'body' => array(
'username' => '######',
'password' => '######'
),
'cookies' => true
)
);
the $response leads me past the login page successfully.
But if I attempt to make a GET call after the fact using $client->get it leads me back to the login page.
How can I POST my login information and then go to another page exportdata.php to then GET form information from it?

I had some trouble using the cookies => true option. have you tried providing a default CooieJar?
Example:
$jar = new GuzzleHttp\Cookie\CookieJar();
$default = [
"cookies" => $jar
];
$c = new GuzzleHttp\Client(["defaults" => $default]);
The same cookie jar is now set by default to each request made by that client.

Related

How can i get my PHP SoapClient to Authenticate with Digest

I currently have a vb.net ASMX web service hosted on IIS along with a PHP page which is calling the web service via a SoapClient.
I need to authenticate the webservice against ActiveDirectory and i figured the easiest way to do this would be to enable Digest Authentication on IIS and allow the user to enter their AD username/password into the PHP page and send this authentication in the SoapHeaders.
I am not really quite sure how to go about this, especially when trying to contact the WSDL (which is also behind the Digest Authentication).
Any help would be appreciated.
Thanks
EDIT: What i've tried:
SERVICE_URL points to http://mypage/service.asmx?wsdl
Attempt 1: User and Pass as MD5
$options = array(
'authentication' => SOAP_AUTHENTICATION_DIGEST,
'realm' => 'myrealm',
'login' => $_SESSION['authUser'],
'password' => $_SESSION['authPass']
);
try { $client = new SoapClient(SERVICE_URL, $options); }
Attempt 2: Auth is the 'user':'realm':'pass' as MD5:
$options = array(
'authentication' => SOAP_AUTHENTICATION_DIGEST,
'login' => $_SESSION['auth']
);
try { $client = new SoapClient(SERVICE_URL, $options); }
You can add headers to your soap client using SoapHeader() class/object (SoapHeader() documentation) and soap object operator __setSoapHeaders() (__setSoapHeaders() documentation).
Your request would look something like this:
<?php
$options = array(
'authentication' => SOAP_AUTHENTICATION_DIGEST,
'realm' => 'myrealm',
'login' => $_SESSION['authUser'],
'password' => $_SESSION['authPass']
);
try {
$client = new SoapClient(SERVICE_URL, $options);
// create and populate header array
$headers = array();
$headers[] = new SoapHeader('MYNAMESPACE',
'authentication',
'SOAP_AUTHENTICATION_DIGEST');
$headers[] = new SoapHeader('MYNAMESPACE',
'realm',
'myrealm');
$client->__setSoapHeaders($headers); // set headers
$client->__soapCall("echoVoid", null); // make soap call
}
?>

How can I send cookie while using REST API?

Using Laravel 5 and trying to send some data from my site to another one, which provides me with the REST API. But they use cookies as a authorization. For this moment, I've passed auth successfully. And stuck on how should I send this cookie to API interface via POST method? Here is my listing.
Thanx in advance.
P.S. All things are going on inside the controller.
if (Cookie::get('amoauth') !== null) {
//COOKIE IS HERE
$client = new Client();
$newlead = $client->post('https://domain.amocrm.ru/private/api/v2/json/leads/set', [
'add' => [
'add/name' => 'TEST LEAD',
'add/date_create' => time(),
'add/last_modified' => time(),
'add/status_id' => '1',
'add/price' => 5000
]
]);
} else {
$client = new Client();
$auth = $client->post('https://domain.amocrm.ru/private/api/auth.php',[
'USER_LOGIN' => 'login',
'USER_HASH' => 'hash',
'type' => 'json'
]);
$auth = $auth->getHeaders('Set-Cookie');
Cookie::queue('amoauth', $auth, 15);
return redirect('/test');
}
Now it returns me the following:
Client error: `POST https://domain.amocrm.ru/private/api/v2/json/leads/set` resulted in a `401 Unauthorized` response.
Found the solution: switched to ixudra/curl.

Cannot authorize PHP NTLMSoapClient with MS Dynamics Great Plains ERP

Having difficulties authorizing php SoapClient with MS Dynamic Great Plains. I can connect through SoapUI. However, it only successfully connects on 3rd attempt. Also, the auth token progressively gets longer. See pastebin link below.
I made use of the following package (https://github.com/mlabrum/NTLMSoap) to setup a NTLM stream, but it doesn't seem to be sending a correct token. The token length is shorter than what is sent through SoapUI.
$wsdlUrl = 'http://example.org:48620/Metadata/Legacy/Full/DynamicsGP.wsdl';
$options = [
'ntlm_username' => 'Domain\username',
'ntlm_password' => 'password'
];
$soapClient = new \NTLMSoap\Client($wsdlUrl, $options);
$params = array(
criteria => array(
'ModifiedDate' => array(
'GreaterThan' => '2016-04-18',
'LessThan' => '2016-04-19'
)
),>
'context' => array(
'OrganizationKey' => array(
'type' => 'CompanyKey',
'Id' =
)
)
);
$soapClient->__setLocation('http://example.org:48620/DynamicsGPWebServices/DynamicsGPService.asmx');
$response = $soapClient->GetPurchaseOrderList(array($params));
I had to set use ___setLocation() because client was being forwarded to http://localmachine:48620/DynamicsGPWebServices/DynamicsGPService.asmx
I have been trying to get Charles Web Proxy to work to show the actual the request/response, buts its crapped out on me.
This is the SoapUI output. http://pastebin.com/7zg4E3qD

Why does Laravel create a new session after a GuzzleHttp POST request to the same subdomain?

I'm trying to make a post request to the same subdomain with GuzzleHttp in a Laravel 5.1 installation, but as a response the login page is returned, showing that a new Session has been created in the request. The current session is not affected.
Why does Laravel create a new session?
In session.php I have the following values:
'driver' => env('SESSION_DRIVER', 'file'),
'lifetime' => 120,
'expire_on_close' => true,
'files' => storage_path('framework/sessions'),
'cookie' => 'admin_mydomain_com_session',
'path' => '/',
'domain' => 'admin.mydomain.com',
'secure' => false
In my controller I use the following code to make the request:
// Create headers
$headers = array(
'X-CSRF-Token' => csrf_token()
);
// Create data
$data = array(
'param' => 'param',
'_token' => csrf_token()
);
// Create a POST request
$client = new Client();
$res = $client->request('POST', 'http://admin.mydomain.com/my-url',
array(
'headers' => $headers,
'form_params' => $data
)
);
$statusCode = $res->getStatusCode();
$body = $res->getBody();
echo $body; // Shows me the login page
The answer is really to understand how sessions work. When you make a request via a browser lets say, the response issued by the server will include a cookie with a session id. That id is what identifies you to the server. When you navigate a site through your browser the request it issues includes the cookies.
So when your creating a request via Guzzle your leaving out the cookie from the previous response. Hence the server will always create a new session id for you.

What's wrong with this PHP code to send POST data via a stream?

I'm trying to use a PHP script to login to another web site (a Moodle install, in this case) by POSTING to the login page.
This code seems to partially work. The problem is that only the first parameter is recognized by the receiving end. For the code shown below, only the 'username' parameter is recognized. However, if you switch the order of 'username' and 'password' so that 'password' is listed first, then only 'password' is recognized using $_POST['password'].
$postdata = http_build_query(
array(
'username' => $currentUser,
'password' => $userPassword,
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($loginURL, false, $context);
Any idea on why only the first parameter is being recognized?
Ok, I see now that the code is correct and that something external is interfering with the POST request. This code was being run inside of Drupal so I think that has something to do with it. When I save this separately as a .php file it works as expected.

Categories