2

I'm trying to create a simple API using PHP but the data doesn't get posted to the file.

$url = "http://localhost/api.php"; $session = curl_init(); curl_setopt($session, CURLOPT_URL, $url); curl_setopt($session, CURLOPT_REFERER, "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); curl_setopt($session, CURLOPT_POST, true); curl_setopt($session, CURLOPT_RETURNTRANSFER, true); //curl_setopt($session, CURLOPT_TIMEOUT, 200); curl_setopt($session, CURLOPT_HEADER, true); curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); curl_setopt($session, CURLOPT_POSTFIELDS, http_build_query(array("process"=>"login","user"=>$_POST['user'],"pass"=>$_PO ST['pass']))); printArr2($session); $result = curl_exec($session); $httpCode = curl_getinfo($session, CURLINFO_HTTP_CODE); curl_close($session); 

The following is the API

if(isset($_RESPONSE['process'])) { if(!strcmp($_RESPONSE['content']['process'],"login")) { $con = dbConnect(); $str = userLogin($con,$_POST['content']['user'],$_POST['content']['pass']); if(is_bool($str)) { $jsonData = json_encode($str); header("HTTP/1.1 200 OK"); header("Content-type: application/json"); echo $jsonData; } else { $jsonData = json_encode($str); header("HTTP/1.1 401 Unauthorised access"); header("Content-type: application/json"); header("Location: ".$_SERVER['HTTP_REFERER']); echo $jsonData; } dbClose($con); exit; } if(!strcmp($_POST['process'],"plagiarism")) { $con = dbConnect(); $user = $_POST['user']; $text = $_POST['text']; } } else { $jsonData = json_encode(array("Error"=>"No methods called")); if(isset($_SERVER['HTTP_REFERER'])) { header('HTTP\1.1 400', true, 400); header("Content-type: application/json"); header("Location: ".$_SERVER['HTTP_REFERER']); echo $jsonData; exit; } else { echo "Invalid entry"; } } 

Whatever I do the output is always the "else" part of isset($_RESPONSE['process']).

I tried putting "process" as get by appending it to the URL.

$url = "http://localhost/checkapi.php?process=login"; 
9
  • It's not posted to what file? You're not saving it to any file. Commented Apr 4, 2016 at 14:54
  • I've pasted the api. check it out Commented Apr 4, 2016 at 16:52
  • I think you have mixed up $_REQUEST with $_RESPONSE. But, as daniel axel writes below, use $_POST instead. Commented Apr 4, 2016 at 17:02
  • {"Host":"localhost","Accept":"\/","Referer":"http:\/\/localhost\/checkapi.php","Content-Type":"plain\/text","Content-Length":"43"} Commented Apr 4, 2016 at 17:12
  • Thank you guys that worked. There's one more thing I'd like your help with. Commented Apr 4, 2016 at 17:24

2 Answers 2

1

You are using an invalid array index (['content']) in your $_RESPONSE and $_POST super globals.

if(!strcmp($_RESPONSE['content']['process'],"login")) { $con = dbConnect(); $str = userLogin($con,$_POST['content']['user'],$_POST['content']['pass']); 

Instead you need:

if(!strcmp($_POST['process'],"login")) { $con = dbConnect(); $str = userLogin($con,$_POST['user'],$_POST['pass']); 
Sign up to request clarification or add additional context in comments.

Comments

1

You are doing a post request, the best way to access those variables is:

vardump($_POST); 

Try printing that in your API before the if so you can debug.

1 Comment

This is the correct answer, but typo: it should be "var_dump": var_dump($_POST);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.