Having trouble getting Fogbugz API response into a SimpleXML object - simplexml

I'm trying to write a wrapper around the fogbugz API, starting with getting a login token. I don's seem to be able to get the token into my wrapper object.
$url = "http://..../fogbugz/api.asp?cmd=logon&email=" . $_UN . "&password=" . $_PW;
$contents = file_get_contents($url);
$resp = simplexml_load_file($contents);
print_r($resp);
Response is: SimpleXMLElement Object ( [token] => SimpleXMLElement Object ( ) ) The object in the token member var is empty. The response string however is OK. If I use
header("Content-type: text/xml");
echo $contents;
I get the correct XML back from the API. Furthermore, if I use the xml as a string, directly in the code it works fine:
$xml = "<?xml version='1.0'?><response><token>iibgo9d785iavs5av5a6lrimbn2r54</token></response>";
$resp = simplexml_load_string($xml);
print_r ($resp);
Response: SimpleXMLElement Object ( [token] => iibgo9d785iavs5av5a6lrimbn2r54 ) Can anyone please tell me how to get the response token into the SimpleXML Object?

I think the XML returned from the API might look like this actually:
<?xml version='1.0'?><response><token><![CDATA[iibgo9d785iavs5av5a6lrimbn2r54]]><token><response>
SimpleXML can't parse CDATA objects.

Related

Laravel $request->getContent() returns empty string

I'm posting a CSV file from one Laravel app to another:
Sending:
$contents = file_get_contents($filePath);
Log::debug('contents', ['contents'=>$contents]); // I can see contents of file
Http::withToken($token)->attach('attachment', $contents)->post($uri);
Receiving:
$content = $request->getContent();
Log::debug('about to store content to file...', ['content'=>$content]);
// about to store content to file... {"content":""}
What am I missing here?
Files are not the content, you should fetch the file out with.
$request->file('attachment');
If you want to send a file as a raw request body, this snippet shows you how from the documentation.
$response = Http::withBody(
file_get_contents($filePath), 'text'
)->post($uri);

How can I set a response using a var?

In the laravel docs it has:
return response($content);
What format is $content?
I want to return it via a var like so:
$res = [null, 204];
return response($res);
But the above fails, just returns a 200.
How can I set a response using a var?
response method like this. response($contents, $statusCode) It's a method of instance Response Object. as you can see that method is getting two parameters. one is contents that you are trying to return to client side. it depending what Content-Type is set in header.
so you can try like this.
$contents = "something whatever";
$statusCode = 204;
return response($contents, $statusCode);
$content can be various things - null, a string, an array.
Status codes are set via a second parameter, not an array element. Passing it an array results in a JSON response, so you're creating a 200 status JSON response of [null,204] with your code.
You fundamentally need to do:
return response(null, 204);
What you can potentially do is generate a response as a variable:
$res = response(null, 204);
and then return $res; elsewhere.

Google Drive API V3 with PHP client 2.0 - download file contents and metadata in a single request

I am trying to download files from Google Drive using PHP client v2.0 with Drive API V3.
Is it possible to retrieve file's body and metadata in a single HTTP request?
Supplying 'alt=media' to ->files->get() returns GuzzleHttp\Psr7\Response upon which I can run ->getBody()->__toString().
If I do not provide 'alt=media', then Google_Service_Drive_DriveFile is returned that has all the metadata, but does not have body.
Question.
Is it possible to get both metadata and body in the same request?
Try something like this:
<?php
// Look at this first: https://developers.google.com/api-client-library/php/auth/web-app
require_once __DIR__ . '/vendor/autoload.php';
//change to your timezone
date_default_timezone_set('Pacific/Auckland');
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setIncludeGrantedScopes(true);
//change scopes to the ones you need
$client->addScope(Google_Service_Drive::DRIVE_FILE, Google_Service_Drive::DRIVE_APPDATA, Google_Service_Drive::DRIVE, Google_Service_Drive::DRIVE_METADATA);
$accessToken = json_decode(file_get_contents('credentials.json'), true);
$client->setAccessToken($accessToken);
//Refresh the token if it's expired. Google expires it after an hour so necessary
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents('credentials.json', json_encode($client->getAccessToken()));
}
$service = new Google_Service_Drive($client);
$results = $service->files->listFiles($optParams);
$fileId = 'yourfileid;
$file = $service->files->get($fileId, array('alt' => 'media'));
file_put_contents("hello.pdf",$file->getBody());
?>

Codeception sendPUT type to Input::file

I'm trying to emulate ajax style uploading of a file in my test. Is it possible to use sendPUT to send a file and return the response? The controller receives the value via Input::file() -- I cannot seem to access what sendPUT sends via Input::file.
$I->sendPUT('/upload_image', array('file' => 'files.jpg'));
You are not using the function correctly.
Everything is explained in the official documentation:
sendPUT
Sends PUT request to given uri.
param $url
param array $params
param array $files
The third parameter is what you are looking for. Your code should look like this:
$I->sendPUT('/upload_image', array(), array('file' => 'files.jpg'));

My data only shows if I debug it. What's going on?

I'm working with Stripe's API and am trying to retrieve data. I have the code below:
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
If I set the AJAX response equal to $data, the response is shown as empty ( {} ). If I debug it in the backend, I get a huge list of all kinds of awesome properties to use. All I do is this:
debug($data); // returns huge data set
The trouble is that I can't access the variable in the frontend. I want to use:
console.log(response);
html += response.url;
And things to that effect, but the data is completely empty when the front end interprets it, for some reason.
In the same effect, I can't set it as a session either (I used to set session logs to debug instead of using the debug feature).
$data // can be accessed on the frontend if we use just php to set a variable
$_SESSION['log'] = $data; // empty
What's going on? I'm using the PHP framework CakePHP 3 (latest version of Beta). I think it has something to do with returning the data as serialized (maybe?) but that wouldn't explain the session logging. This happens right before we send the data back:
$this->set(compact('data', $data));
$this->set('_serialize', 'data');
If $data is not empty than you should just use the set method in the controller
$this->set(compact('data', $data));
Than you should have the corresponding view at /src/Template/ControllerName/json/methodName.ctp (Change ControllerName and methodName to what you have)
This file should be this.
<?php
print json_encode($data);
?>
That is all. You should have your data on your client side as a json object.
Turns out the answer was that the values could not be displayed while the array was protected. Calling Stripe's method __toArray() on the Stripe object made the data accessible, and setting worked past this point.
$data = \Stripe_Invoice::all(array(
"customer" => $user->customer_id
));
$data = $data->__toArray();
$this->set(compact('data', $data));
$this->set('_serialize', 'data');

Resources