【问题标题】:How to make a cURL request to phpcodechecker?如何向 phpcodechecker 发出 cURL 请求?
【发布时间】:2022-11-02 21:57:17
【问题描述】:

我正在尝试通过使用 cURL 来使用 phpcodechecker.com。 示例请求如下所示:

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

我的输出总是返回null。 它与我如何传递代码参数有什么关系吗?

$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 文档

通过 GET 或 POST 使用变量名称向 API 提交代码:code

示例(无错误):http://phpcodechecker.com/api/?code=$hello;

使用 POST 时,您可以 base64_encode() 然后 urlencode(),只需 在您的请求 url 或正文中传递 http://phpcodechecker.com/api/?base64 内容

【问题讨论】:

  • 好的,但是你有问题要问
  • 是的,返回值为空。一个 JSON 是预期的。
  • 您可能应该使用单引号而不是双引号创建字符串,否则它将尝试解析出$string
  • 在 curl 代码中,您似乎将 $code 放入标头,而不是查询参数(因为它在您的示例请求 URL 中)。不知道你为什么这样做。你了解 HTTP 请求的基本结构吗?例如URL、查询参数、标题、正文等?
  • 你得到null 因为$data 不是json。它正在返回网站的 HTML

标签: php php-curl


【解决方案1】:

工作代码:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 2018-04-23
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多