I'm struggling to get a web service to work which we are using to integrate with a software package from Civica. Unfortunately they have sent us code in visual basic which we don't use here and I'm struggling to "translate" the visual basic into PHP.
So here's the code I have so far:
error_reporting(E_ALL);
$client = new SoapClient('https://xxx/cx/WebService/WSTokenService?
singleWsdl');
var_dump($client->__getFunctions());
The above works and I can see the functions. It's the next bit I can't get to work:
$request_param = array('Password' => 'xxx', 'UserName' => 'yyy');
try
{
$request = $client ->GetUserWSToken($request_param);
$result = $request ->GetUserWSTokenResult;
print_r($result);
}
catch (Exception $e)
{
echo "<P><P>Exception Error!";
echo $e->getMessage();
}
When I run the above code I get an error message "Exception Error!GetUserWSToken has service faults". I also get another error "Object reference not set to an instance of an object".
The visual basic code we've been sent by the package company to use is:
Dim tokenClient As New TokenService.WSTokenServiceClient
Dim tokenreq As New TokenService.SecurityTokenRequest
tokenreq.UserName = "usernamehere"
tokenreq.Password = "passwordhere"
Dim token = tokenClient.GetUserWSToken(tokenreq)
Does anyone know how to translate the visual basic above into what I need to do for PHP?
Related
I am trying to implement the new "Sign in with Google" button as described in https://developers.google.com/identity/gsi/web/guides/display-button.
Everything is fine, and I am able to get a response from the button with "credential" and "g_csrf_token" elements, which I can send to my server. However, using the Google API Client to decode the credential doesn't work. I'm trying to follow the instructions.
Here's my code:
$id_token = filter_input(INPUT_POST, 'credential');
$csrfToken = filter_input(INPUT_POST, 'g_csrf_token'); //??? Do we need this?
$client = new Google_Client(['client_id' => $clientid]);
$client->addScope("email"); // Recommended in another StackOverflow answer but makes no difference
try {
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
The result is the error message id_token must be passed in or set as part of setAccessToken.
I've updated my Google API Client to v2.11.
I assume that I've missed a step somewhere - can someone help?
Have found a solution, by trial and error! Turns out that $id_token needs to be passed to the client twice, once in setAccessToken() and then again in verifyIdToken(). Omitting setAccessToken fails (like the error message says), but if you pass it in setAccessToken but NOT in verifyIdToken, that doesn't work either.
$id_token = filter_input(INPUT_POST, 'credential');
$client = new Google_Client(['client_id' => $clientid]);
try {
$client->setAccessToken($id_token);
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload
It would nice, if you're at Google and picking this up, if you updated the documentation.
Twinfield is an online financial accounting software package for small and medium enterprises, produced and maintained by Twinfield International, based in The Netherlands. It is used by over 15,000 subscribers in 20 countries.
I want to integrate its API. I have install laravel and create some basic API, but its huge. How and where the integration example links? Please help me.
This is not the full code but gives you the login for the twinfield. I am also stuck because many libraries are given for twinfield but not give any sample code for it. There is no document provide for PHP integration. I am very disappointed with twinfield. Even if you have test account and it will disable than it will permanent disable. Here the jsonresponse is custom made so you can call only $e->getMessage() if you have any error related it.
public function login(\Illuminate\Http\Request $request){
$user = $request->input('user');
$password = $request->input('password');
$org = $request->input('organisation');
$params = array(
'user' => $user,
'password' => $password,
'organisation' => $org
);
// login => Set the param array and send to the logon
try
{
$session = new \SoapClient("https://login.twinfield.com/webservices/session.asmx?wsdl", array('trace' => 1));
$result = $session->logon($params);
// echo '<pre>';print_r($result);
$cluster = $result->cluster;
$qq = new domDocument();
$qq->loadXML($session->__getLastResponse());
$sessionID = $qq->getElementsByTagName('SessionID')->item(0)->textContent;
//echo $sessionID;
$newurl = $cluster . '/webservices/processxml.asmx?wsdl';
try
{
$client = new \SoapClient($newurl);
$data = new \SoapHeader('http://www.twinfield.com/', 'Header', array('SessionID'=> $sessionID));
$jsonResponse = JsonResponse::success($data);
}
catch (SoapFault $e)
{
$jsonResponse = empty($e->getMessage()) ? JsonResponse::error(class_basename($e)) : JsonResponse::error($e->getMessage());
}
}
catch (SoapFault $e)
{
$jsonResponse = empty($e->getMessage()) ? JsonResponse::error(class_basename($e)) : JsonResponse::error($e->getMessage());
}
return $jsonResponse;
}
Some code are given in this link too. You will integrate it via https://github.com/php-twinfield/twinfield but you have to work a lot. I am also working on it, if anything you need plz let me know.
I tried to read/download a test file called "test.jpg" from my bucket on Google Cloud Storage via the Google Cloud PHP API.
I tried to read the file via:
try
{
$object = $storageService->objects->get( $bucket, $file_name );
$request = new Google_Http_Request($object['mediaLink'], 'GET');
$signed_request = $client->getAuth()->sign($request);
$http_request = $client->getIo()->makeRequest($signed_request);
echo $http_request->getResponseBody();
}
catch (Exception $e)
{
print $e->getMessage();
}
The API Call works fine (as I can see in the Google Cloud API Console), but as a response I only get a full page with something like that:
What am I making wrong?
I'm trying to copy a remote file (image PNG, GIF, JPG ...) to my server. I use Guzzle since I sometimes get 404 with copy() even if the file exists and I also need to do a basic auth. This script is within a long script launched in command triggered by a cron job.
I'm pretty new to Guzzle and I successfully copy the image but my files have wrong mime type. I must be doing something wrong here. Please suggest me a good way to do this (including checking success/failure of copy and mime type check). If file has no mime type I would pop an error with details informations.
Here is the code:
$remoteFilePath = 'http://example.com/path/to/file.jpg';
$localFilePath = '/home/www/path/to/file.jpg';
try {
$client = new Guzzle\Http\Client();
$response = $client->send($client->get($remoteFilePath)->setAuth('login', 'password'));
if ($response->getBody()->isReadable()) {
if ($response->getStatusCode()==200) {
// is this the proper way to retrieve mime type?
//$mime = array_shift(array_values($response->getHeaders()->get('Content-Type')));
file_put_contents ($localFilePath , $response->getBody()->getStream());
return true;
}
}
} catch (Exception $e) {
return $e->getMessage();
}
When I do this my mime type is set to application/x-empty
Also it looks like when status is different from 200 Guzzle will automatically throw an exception. How can I stop this behaviour and check status myself so I can custom error message?
EDIT: This was for Guzzle 3.X
Now this is how you can do it using Guzzle v 4.X (works as well with Guzzle 6)
$client = new \GuzzleHttp\Client();
$client->get(
'http://path.to/remote.file',
[
'headers' => ['key'=>'value'],
'query' => ['param'=>'value'],
'auth' => ['username', 'password'],
'save_to' => '/path/to/local.file',
]);
Or using Guzzle stream:
use GuzzleHttp\Stream;
$original = Stream\create(fopen('https://path.to/remote.file', 'r'));
$local = Stream\create(fopen('/path/to/local.file', 'w'));
$local->write($original->getContents());
This looks great. Is there better/proper solution when using Guzzle 4?
Your code can be simplified a great deal. My example code below will stream the body of the response directly to the filesystem.
<?php
function copyRemote($fromUrl, $toFile) {
try {
$client = new Guzzle\Http\Client();
$response = $client->get($fromUrl)
->setAuth('login', 'password') // in case your resource is under protection
->setResponseBody($toFile)
->send();
return true;
} catch (Exception $e) {
// Log the error or something
return false;
}
}
When I do this my mime type is set to application/x-empty
A filesystem mimetype?
Also it looks like when status is different from 200 Guzzle will automatically throw an exception. How can I stop this behaviour and check status myself so I can custom error message?
Guzzle will throw an exception for bad responses like 4xx and 5xx. No need to disable this. Just catch an exception and deal with the error there.
Look at this with post:
$myFile = fopen('path/to/file', 'w') or die('Problems');
$client = new \Guzzle\Service\Client();
$request = $client->post('https://www.yourdocumentpage.com', array(), ['pagePostField' => 'data'], ['save_to' => $myFile]);
$client->send($request);
fclose($myFile);
here you must send the request of your "post"
and with get
$myFile = fopen('path/to/file', 'w') or die('Problems');
$client = new \GuzzleHttp\Client();
$request = $client->get('https://www.yourdocumentpage.com', ['save_to' => $myFile]);
and here you don't need to send the request,
and here you'll find a lot of documentation, you must have guzzle 6 for doing that, and if you are using GOUTTE at the same time you'll need goutte 3.1, update your require in your composer.json
using Guzzle 6 just use SINK option. see below detailed function
Extra:
use GuzzleHttp\Client; Guzzle namespace included
$access_token = if you need auth else simply remove this option
ReportFileDownloadException = custom exception
/**
* download report file and read data to database
* #param remote url
* #return N/A
* #throws ReportFileDownloadException
*/
protected function getReportFile($report_file_url)
{
$file = $this->tempDirectory . "/" . basename($report_file_url);
$fileHandle = fopen($file, "w+");
try {
$client = new Client();
$response = $client->get($report_file_url, [
RequestOptions::SINK => $fileHandle,
RequestOptions::HEADERS => [
"Authorization" => "Bearer $access_token"
]
]);
} catch (RequestException $e) {
throw new ReportFileDownloadException(
"Can't download report file $report_file_url"
);
} finally {
#fclose($fileHandle);
}
throw new ReportFileDownloadException(
"Can't download report file $report_file_url"
);
}
I am trying to send push notifications to my android application using PHP server with Zend framework, but i am getting the HttpResponse as : The request could not be parsed as JSON or contains invalid fields, with the error code 400.
I have used the server code as given in the demo of Zend framework, which is as follows :
<?php
require_once 'Zend/Mobile/Push/Gcm.php';
require_once 'Zend/Mobile/Push/Message/Gcm.php';
$regId = $_POST["registrationid"];
$message = new Zend_Mobile_Push_Message_Gcm();
$message->setId(time());
$message->addToken($regId);
$message->setData(array(
'foo' => 'bar',
'bar' => 'foo',
));
$gcm = new Zend_Mobile_Push_Gcm();
$gcm->setApiKey('my api key');
try {
$response = $gcm->send($message);
} catch (Zend_Mobile_Push_Exception $e) {
// all other exceptions only require action to be sent or implementation of exponential backoff.
die($e->getMessage());
}
// handle all errors and registration_id's
foreach ($response->getResults() as $k => $v) {
if ($v['registration_id']) {
printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
}
if ($v['error']) {
printf("%s had an error of: %s\r\n", $k, $v['error']);
}
if ($v['message_id']) {
printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
}
}
I don't have my exact URL handy and I'm trying on my phone, but I touched on this recently in my blog. See http://www.rogerethomas.com
What you will need is to use a browser API key, not a server one. Don't ask why but it seems there's an error with Googles documentation.
Edit: here's a recent link to my answer from another similar question:
GCM with PHP (Google Cloud Messaging)