Friday, June 3, 2016

PHP: External Service Sends JSON Format to your API Method

If you have an external service sending information over to your PHP server as JSON or XML, you can capture the response as follows:

$postResult = file_get_contents('php://input');
$payload = json_decode($postResult, true);

PHP: XML File Reading using Simple XML

The simplest solution is to load the file and process the xml nodes as follows:

$xml = simplexml_load_file('myfile.xml');

$node1 = $xml->child1;

$node2 = $xml->child2;


However, in some instances the resulting xml is either blank or empty. In this case, we need to call the following method:

$xml = simplexml_load_file('myfile.xml');

echo $xml->asXML();


If none of the above steps work, we need to create a Simple XML Element as follows:


$content = file_get_contents($fileName);

$xml = new SimpleXMLElement($content);
$node1 = $xml->child1;

If you still have issues after all this, typecast the node values:

$node1 = (string)$xml->child1;


Sample XML: myfile.xml
<root>

   <child1>Child 1</child1>

   <child2>Child 2</child2>

</root>

Wednesday, June 1, 2016

GIT: HTTPS Repository Error

If you get the following error while pushing to a specific remote repository: The requests URL returned error: 401 Unauthorized while accessing https:...

Try adding the repository as follows first then push:

git remote add origin https://yourname@bitbucket.org/yourorg/yourrepository
......
git push -u origin

GIT: Remove Origin from Git Repository

In case you accidentally added an incorrect remote or you want to switch from ssh to https or vice versa, you can try the following command:

git remote rm origin

GIT: Configure Global User

In case you need to set up both the global user name and email in git, you need to provide the following commands:

git config --global user.name "Your Name"

git config --global user.email you@example.com

Please be careful when executing the above commands as these will override the current settings.

In case you want to commit/push as a different user without affecting the global settings, you can try:

git commit --amend --author='Your Name <you@example.com>'

For more information on git Commands: https://git-scm.com/docs

GIT: From an Existing Project

cd /path/to/my/repogit remote add origin git@bitbucket.org:myorg/myrepository.git(or git remote add origin https://bitbucket.org/myorg/myrepository) in case you use httpsgit push -u origin --all # pushes up the repo and its refs for the first timegit push origin --tags # pushes up any tags
More info from BitBucket

GIT: Creating a GIT Repository From Scratch

Set up your local directory

mkdir /path/to/your/projectcd /path/to/your/projectgit initgit remote add origin git@bitbucket.org:myorg/myrepository.git (or git remote add origin https://bitbucket.org/myorg/myrepository) in case you use https

Create your first file, commit, and push

echo "My Text" >> contributors.txtgit add contributors.txtgit commit -m 'Initial commit with contributors'git push -u origin master

You can also find this information when creating a new repository from BitBucket [1].[1] https://bitbucket.org