user2800050 user2800050 - 6 months ago 13
PHP Question

Making Ideone API work on Codeigniter (ajax, javascript,soapclient)

This is my first time to use Codeigniter. So sorry if my question is noob.
I got codes from this site: Working with IDE One API (Full project code is here) and now, I am trying to use it in codeigniter.

I was able to make it work up to the javascript part below (since it was able to check if 'source' has value).

script.js:

$(document).ready(function() {
$('#myform').submit( function(){
var data = $(this).serialize();
var source = $('textarea#source').val();

if( source == '' ) {
alert( 'No source code provided');
return false;
}

$(this).append('<div class="loading">Processing...</div>');

$.ajax({
type: "POST",
url: "<?php echo base_url();?>student/submit",
dataType: 'json',
data: data + '&process=1',
cache: false,
success: function(response){
$('.loading').remove();
$('.cmpinfo').remove();
$('#response').show();
//alert(response);
console.log(response.raw);
if( response.status == 'success' ) {
$('.meta').text( response.meta );
$('.output').html('<strong>Output</strong>: <br><br><pre>' + response.output + '</pre>');

if( response.cmpinfo ) {
$('.cmpinfo').remove();
$('.meta').after('<div class="cmpinfo"></div>');
$('.cmpinfo').html('<strong>Compiler Info: </strong> <br><br>' + response.cmpinfo );
}

} else {
//$('.output').html('<pre>' + response + '</pre>');
alert( response.output );
}
//alert( response.msg );
}
});

return false;
});
});


My problem is I'm not sure whether it is able to reach the function "submit" in my student

controller or the problem lies in the function itself.

student/submit:

public function submit(){
$user = 'myuser';
$pass = 'mypassword';
$code = '';
$input = '';
$run = true;
$private = false;

$subStatus = array(
0 => 'Success',
1 => 'Compiled',
3 => 'Running',
11 => 'Compilation Error',
12 => 'Runtime Error',
13 => 'Timelimit exceeded',
15 => 'Success',
17 => 'memory limit exceeded',
19 => 'illegal system call',
20 => 'internal error'
);

$error = array(
'status' => 'error',
'output' => 'Something went wrong :('
);

//echo json_encode( array( 'hi', 1 ) ); exit;
//print_r( $_POST ); exit;

if ( isset( $_POST['process'] ) && $_POST['process'] == 1 ) {
$lang = isset( $_POST['lang'] ) ? intval( $_POST['lang'] ) : 1;
$input = trim( $_POST['input'] );
$code = trim( $_POST['source'] );

$client = new SoapClient( "http://ideone.com/api/1/service.wsdl" );

//create new submission
$result = $client->createSubmission( $user, $pass, $code, $lang, $input, $run, $private );

//if submission is OK, get the status
if ( $result['error'] == 'OK' ) {
$status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
if ( $status['error'] == 'OK' ) {

//check if the status is 0, otherwise getSubmissionStatus again
while ( $status['status'] != 0 ) {
sleep( 3 ); //sleep 3 seconds
$status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
}

//finally get the submission results
$details = $client->getSubmissionDetails( $user, $pass, $result['link'], true, true, true, true, true );
if ( $details['error'] == 'OK' ) {
//print_r( $details );
if ( $details['status'] < 0 ) {
$status = 'waiting for compilation';
} else {
$status = $subStatus[$details['status']];
}

$data = array(
'status' => 'success',
'meta' => "Status: $status | Memory: {$details['memory']} | Returned value: {$details['status']} | Time: {$details['time']}s",
'output' => htmlspecialchars( $details['output'] ),
'raw' => $details
);

if( $details['cmpinfo'] ) {
$data['cmpinfo'] = $details['cmpinfo'];
}

echo json_encode( $data );
} else {
//we got some error :(
//print_r( $details );
echo json_encode( $error );
}
} else {
//we got some error :(
//print_r( $status );
echo json_encode( $error );
}
} else {
//we got some error :(
//print_r( $result );
echo json_encode( $error );
}
}

}


The View part:

<div id="wrapper">
<h1><a href="index.php">Online Compiler</a></h1>
<?php echo form_open("student/submit",array('id' => 'myform')); ?>
<div>
<label for="lang">Select Language:</label>
<select name="lang" id="lang">
<option value="7 ">Ada (gnat-4.3.2)</option>
<option value="13">Assembler (nasm-2.07)</option>
<option value="45">Assembler (gcc-4.3.4)</option>
<option value="104">AWK (gawk) (gawk-3.1.6)</option>
<option value="105">AWK (mawk) (mawk-1.3.3)</option>
<option value="28">Bash (bash 4.0.35)</option>
<option value="110">bc (bc-1.06.95)</option>
<option value="12">Brainf**k (bff-1.0.3.1)</option>
<option value="11">C (gcc-4.3.4)</option>
<option value="27">C# (mono-2.8)</option>
<option value="1" selected="selected">C++ (gcc-4.3.4)</option>
<option value="44">C++0x (gcc-4.5.1)</option>
<option value="34">C99 strict (gcc-4.3.4)</option>
<option value="14">CLIPS (clips 6.24)</option>
<option value="111">Clojure (clojure 1.1.0)</option>
<option value="118">COBOL (open-cobol-1.0)</option>
<option value="106">COBOL 85 (tinycobol-0.65.9)</option>
<option value="32">Common Lisp (clisp) (clisp 2.47)</option>
<option value="102">D (dmd) (dmd-2.042)</option>
<option value="36">Erlang (erl-5.7.3)</option>
<option value="124">F# (fsharp-2.0.0)</option>
<option value="123">Factor (factor-0.93)</option>
<option value="125">Falcon (falcon-0.9.6.6)</option>
<option value="107">Forth (gforth-0.7.0)</option>
<option value="5">Fortran (gfortran-4.3.4)</option>
<option value="114">Go (gc-2010-07-14)</option>
<option value="121">Groovy (groovy-1.7)</option>
<option value="21">Haskell (ghc-6.8.2)</option>
<option value="16">Icon (iconc 9.4.3)</option>
<option value="9">Intercal (c-intercal 28.0-r1)</option>
<option value="10">Java (sun-jdk-1.6.0.17)</option>
<option value="35">JavaScript (rhino) (rhino-1.6.5)</option>
<option value="112">JavaScript (spidermonkey) (spidermonkey-1.7)</option>
<option value="26">Lua (luac 5.1.4)</option>
<option value="30">Nemerle (ncc 0.9.3)</option>
<option value="25">Nice (nicec 0.9.6)</option>
<option value="122">Nimrod (nimrod-0.8.8)</option>
<option value="43">Objective-C (gcc-4.5.1)</option>
<option value="8">Ocaml (ocamlopt 3.10.2)</option>
<option value="119">Oz (mozart-1.4.0)</option>
<option value="22">Pascal (fpc) (fpc 2.2.0)</option>
<option value="2">Pascal (gpc) (gpc 20070904)</option>
<option value="3">Perl (perl 5.12.1)</option>
<option value="54">Perl 6 (rakudo-2010.08)</option>
<option value="29">PHP (php 5.2.11)</option>
<option value="19">Pike (pike 7.6.86)</option>
<option value="108">Prolog (gnu) (gprolog-1.3.1)</option>
<option value="15">Prolog (swi) (swipl 5.6.64)</option>
<option value="4">Python (python 2.6.4)</option>
<option value="116">Python 3 (python-3.1.2)</option>
<option value="117">R (R-2.11.1)</option>
<option value="17">Ruby (ruby-1.9.2)</option>
<option value="39">Scala (scala-2.8.0.final)</option>
<option value="33">Scheme (guile) (guile 1.8.5)</option>
<option value="23">Smalltalk (gst 3.1)</option>
<option value="40">SQL (sqlite3-3.7.3)</option>
<option value="38">Tcl (tclsh 8.5.7)</option>
<option value="62">Text (text 6.10)</option>
<option value="115">Unlambda (unlambda-2.0.0)</option>
<option value="101">Visual Basic .NET (mono-2.4.2.3)</option>
<option value="6">Whitespace (wspace 0.3)</option>
</select>
</div>

<div>
<label for="source">Source Code:</label>
<textarea cols="40" rows="10" name="source" id="source"></textarea>
</div>

<div >
<label for="input">Input: <span class="description">(Data that will be given to the program on the stdin.)</span></label>
<textarea cols="40" rows="3" name="input" id="input"></textarea>
</div>

<div>
<input type="submit" name="submit" value="Submit" />
</div>
<?php echo form_close(); ?>

<div id="response">
<div class="meta"></div>
<div class="output"></div>
</div>


</div>


It just stays on the part where the "Processing" shows and nothing happens after that. Please help me. Thank you so much :(

I'm not sure if this would help solve the problem. But when I traced the Network Requests, I am getting "Disallowed Key Characters" as a "Response". I assume this is because of the Request URL:

//localhost/ci_user/index.php/student/%3C?php%20echo%20base_url();%20?%3Estudent/submit


Am I getting anywhere? Pls help

Answer

I got it! Thanks to a friend who taught me about that "CTR+SHIFT+I" of Chrome. The Request URL really is the error.

I just changed this part in the script's AJAX post request:

 url: "<?php echo base_url();?>student/submit",

to

    url: "submit",

The response/error sent by CI which is "Disallowed Key Characters" was because of the excess php codes getting mixed up with the Request URL ( the one with base_url() ).

Thanks to this problem(no matter how simple this is), I was able to have a very nice introduction on codeigniter workarounds.