smolo
smolo

Reputation: 879

How to make a cURL request to phpcodechecker?

I'm trying to use phpcodechecker.com by using cURL. A sample request would look like this:

http://phpcodechecker.com/api/?code=$hello;

My output always returns null. Does it have anything to do with how I pass the code parameter?

$code = "
    function test($string){
        return $string ;
    }
";


$url = "http://phpcodechecker.com/api/";


$request_headers = array(
    "code:" . base64_encode($code),
    'Content-Type:application/x-www-form-urlencoded'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_VERBOSE, true);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// curl_setopt($ch, CURLOPT_POST, TRUE);
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); // Might need this, but I was able to verify it works without
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');

$data = curl_exec($ch);

if (curl_errno($ch)) {
    print "Error: " . curl_error($ch);
    exit();
}

$json = json_decode($data, true);

curl_close($ch);

var_dump($json);

API documentation

Submit code to the API by GET or POST with variable name: code

Example (no errors): http://phpcodechecker.com/api/?code=$hello;

You can base64_encode() and then urlencode() when using POST, just pass http://phpcodechecker.com/api/?base64 in your request url or body content

Upvotes: 0

Views: 29

Answers (1)

smolo
smolo

Reputation: 879

Working code:

$code = '
    function test($string){
        return error_here;
    }
';


$ch = curl_init();

$url = "http://phpcodechecker.com/api/";
$dataArray = ['code' => base64_encode($code)];

$data = http_build_query($dataArray);

$getUrl = $url."?".$data;

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $getUrl);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
   
$response = curl_exec($ch);
    
if(curl_error($ch)){
    echo 'Request Error:' . curl_error($ch);
}else{
    echo $response;
}
   
curl_close($ch);

Upvotes: 1

Related Questions