Friday, August 24, 2018

$_POST array empty in PHP after an a request with post data

I am sending parameters using this method to my server php but I get the values that you post shipping:

function post(path, parameters) {
var http = new XMLHttpRequest();
console.log(parameters);
http.open("POST", path, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(parameters);
}

php :

public function tracking_referidos(){
    $this->autoRender = false;
    $result = array();
    $result['post'] = $_POST;
    echo json_encode($result);
    exit;
}

result : {"post":{"referrer":"","event":"eventrid","hash":"45hfkabdus"}}

Solved

You're sending a JSON string. PHP doesn't decode that data and map it to the $_POST super global automatically. If you want PHP to do that, you need to send the data as application/x-www-form-urlencoded (ie similar to the URI of a get request: key=value&key2=value2).

You can send data using the applicatio/json header, but to get at the request data, you need to read the raw post body. You can find that in the php://input stream. Just use file_get_contents to read it:

$rawPostBody = file_get_contents('php://input');
$postData = json_decode($rawPostBody, true);//$postData is now an array

Monday, August 20, 2018

swift, objective-c protocol implementation

Still trying to get used to swift, but since my obj-c knowledge is close to 0, I have not been able to implement this SocketRocket protocol. Any help would be greatly appreciated

Here's the obj-c delegate I try to implement

@protocol SRWebSocketDelegate 

// message will either be an NSString if the server is using text
// or NSData if the server is using binary.
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;

@optional

- (void)webSocketDidOpen:(SRWebSocket *)webSocket;
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;

@end

I hoped this was the proper way to implement it; it wasn't...

I get 'SocketDelegate' does not conform to protocol 'SRWebSocketDelegate'

class SocketDelegate:UIViewController, SRWebSocketDelegate{
    let socket:SRWebSocket! = SRWebSocket()

    override func loadView() {
        self.socket.delegate = self
    }    

    func didReceiveMessage(message:AnyObject){

    }
}

Solved

The answer is:

func webSocket(webSocket: SRWebSocket!, didReceiveMessage message: AnyObject!)

see

Functions in Swift Reference Book

Method name in Obj-C webSocket:didReceiveMessage is translated such as the first part is the method name, the other parts are the external parameter names (didReceiveMessage). Also note that id becomes AnyObject and Obj-C references are translated with ! as implicitly unwrapped optionals (this is no longer true, implicitly unwrapped optionals are now rare thanks to attributes added to Obj-C declarations).


An alternative solution: try Starscream - a native Swift Websocket library.


Sunday, August 19, 2018

Resolving Merge Conflict in IDE Whilst Retaining 3-Way Merge Info

(I am using SourceTree as my Git tool, Beyond Compare to resolve merge conflicts, and Eclipse as my Java IDE.)

The conflict is complex enough that I cannot solve it in the merge tool, and will have to resolve it manually (across multiple files).

Ideally, I'd like to just take my version of the code, look at the conflicts in Beyond Compare, and use that to advise my changes in Eclipse.

Things that don't work:

  1. If I just open up the conflicting version in Eclipse, then the code doesn't compile, so I lose all of the usefulness of static type checking etc.
  2. If I just "resolve using ours/mine" in Git (SourceTree) so that I can open it in Eclipse, then I lose all of the nice three-way merge information that I could have used to resolve the conflict.
  3. Doing a naive diff of the HEAD version of the file with the master version does not solve my problem either - the diff has too little information. I want to have the three-way info.

Am I approaching this the wrong way? Should I be using a different strategy to resolve conflicts? Is there perhaps a feature of Git or BC4 that I could use to keep the conflict info separately whilst editing a specific version of the file?

Solved

Something I've found that works (inspired by Stun Brick's suggestion):

Start the merge, so that the LOCAL, BASE, and REMOTE files are generated.

Move these files out to a separate directory.

Resolve the merge by taking the local copy (In SourceTree: Resolve Conflicts -> Resolve Using Mine. In Git: git checkout --ours PATH/FILE).

Open the local version of the file in Eclipse.

Open the LOCAL, BASE, and REMOTE files in BC4.

Now I can edit in Eclipse to my heart's content without affecting what BC4 displays.


Personally, I would copy the THEIRS version into Eclipse and re-introduce the local changes from there. If your MINE includes significantly more changes, or refactors a lot of code, it might be better to start with that.

This could be done this way:

  1. Open the in-progress merge files in Eclipse (with it's silly merge tags)
  2. View the THEIRS commit in Eclipse, (using a git plugin),
  3. Open the specific commit's version of the files (no merge tags)
  4. Copying everything over.
  5. Open the MINE commit's version as well and re-introduce your changes, manually continuing the merge.

This should allow you to start with a working MINE or THEIRS, adding to it piece by piece.

This would allow you to still use suggestions from BC for the most significantly changed file. Since git only opens the external merge for one folder at a time you'd have to manually open the other files in BC if you feel you need that.

You can then manually mark each file as "resolved".


Friday, August 17, 2018

Installing mariaDB on mac

I'm using mac ports to install the mariaDB with the following command:

sudo port install mariadb-server

After the file is installed, I have no idea what's going on next?

I try to find any configuration guidance but I failed, does anybody have some guidance for the next steps after installing from mac ports (like how to start and stop, configure etc)

Solved

Install db

sudo -u _mysql /opt/local/lib/mariadb/bin/mysql_install_db

Change root password

/opt/local/lib/mariadb/bin/mysqladmin -u root password 'new-password'

Alternatively you can run:

/opt/local/lib/mariadb/bin/mysql_secure_installation'

You can start the MariaDB daemon with:

cd /opt/local; /opt/local/lib/mariadb/bin/mysqld_safe --datadir='/opt/local/var/db/mariadb'

The configuration will generally end up in /opt/local/ somewhere...

You will probably need to

$ sudo port load mariadb-server

, to start it. And

$ sudo port unload mariadb-server

, to stop it.

This is just the way the macports stuff usually works - I haven't tried it with mariadb specifically...

I hope this helps a little...


You can install it via Homebrew, It's very easy

⚠️ You should have Homebrew installed

 2. $ brew update
 3. $ brew search mariadb
 4. $ brew install mariadb

Once installed, you will need to activate it with :

$ brew services start mariadb

Or, if you don't want/need a background service you can just run:

$ mysql.server start