PHP JSON interaction

I realize this is more a php question, but it has to do with interfacing with a Xojo built web application.

I have build a php application that needs to interface with a Xojo Web App. I am looking to call a special uRL, and collect data from the response and/or cause an action in the Web App.

The Web app is up and functional. So the Xojo end - I believe - is functioning correctly.

I am looking for a references on performing this type of operation. Perhaps this has been discussed, and I haven’t located it.

I have thought to post with a cURL and/or run with a JSON_decode of a get_file_contents in php. With a cURL:

		function curl_request($url,  $postdata = false) //single custom cURL request.
		{
			
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_HEADER, TRUE); 
			curl_setopt($ch, CURLINFO_HEADER_OUT, true);
			curl_setopt($ch, CURLOPT_VERBOSE, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);     

			curl_setopt($ch, CURLOPT_URL, $url);
		
		
			if ($postdata)
			{
				curl_setopt($ch, CURLOPT_POST, true);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);   
			}
		
			$response = curl_exec($ch);
		
			curl_close($ch);
		
			return $response;
		}

		$PostData = "something";
		
		$data = curl_request("MyWebApp_SPECIAL_URL", $PostData);
	
		var_dump($data);

This returns

string(63) "HTTP/1.1 405 OK Content-Type: text/html Content-Length: 0 "

Does this seem like the right way? Or should I be using:

$json = file_get_contents("MyWebApp_SPECIAL_URL" . $PostData); $data = json_decode($json); var_dump($data)

Or maybe I am way off base and there is a better recommendation on php app inter acing with a Xojo Web Service.

Many thanks in advance!!

What are you doing in your Xojo Web App’s HandleSpecialURL?

It looks like you’re not setting the type on the response, not printing anything or not returning true.

I’m not sure about typing the response. I maybe I’m missing something, but php is a loosely typed language and so you don’t type anything.

Essentially I’m reaching out to anyone who has ever had to write PHP to interact with a Xojo Web application via CURL. Any advice and/or direction on accomplishing this?

So after working on this the better part of the day I did get things to work. So I though I would relay my success here, in case anyone out there tries to do the same.

I found that in php it is best to use cURL to interact with the a Xojo Web App instead of the get_file_contents. the cURL is much better performance. Here’s my working code for getting data from a Xojo Wep App using php/cURL.

[code]
// Setting up data for the Web App to parse in the Special Handler. These first two lines building in the post url will differ depending on what the Web App is expecting.
$Authorization = “Authorization=” . base64_encode(“username:password”);
$URLBase = “my_web_app_SPECIAL_url”;

function curl_request($url,  $postdata) //single custom cURL request.
{
	$ch = curl_init();
	curl_setopt_array($ch, array(
		CURLOPT_URL => $url . "?" . $postdata,
		CURLOPT_RETURNTRANSFER => true
	));
	
	$response = curl_exec($ch);
	if (!curl_exec($ch)) {
		// if curl_exec() returned false and thus failed
		echo '<br>An error has occurred: ' . curl_error($ch). '<br>';
	}
	else {
		echo '<br>everything was successful<br>';
	}
	
	echo  "<br>cURL Get Info returns : <br>";
	print_r(curl_getinfo($ch));
	
	curl_close($ch);
	
	return $response;
}

$data = curl_request($URLBase, $Authorization); //return cURL request to $data

//Display request result
echo "<br><br>cURL request returns : <br>";
var_dump($data);[/code]

From there you can intricate the $data array for the data you need from the Web service.

Once I started getting expected 401 and 405 messages from the Xojo app, and was able to track the secondary issue down in the Xojo debugger.

I should have been clearer, I meant the http Content-Type (http://documentation.xojo.com/index.php/WebRequest.MIMEType).