0

Sorry, I really don't know how to write the title of my question

I am doing a PHP process using third parties API demo, in my computer (XAMPP) working well, but when I upload to my website, will be 502 error.

I have 2 files, class.php (API demo from third parties) and my.php

In class.php

class QF_HTTP_CLIENT{
    public $domain = ''; // 站点hostname
    public $token = ''; // 开放平台token

    /**
     * @param string $method 请求方式
     * @param string $path 请求路径
     * @param array $requestBody 请求体参数
     * @return array
     * @throws Exception
     */
    private function _request($method, $path, $requestBody)
    {
        $header = array(
            "Authorization: Bearer {$this->token}",
            "Content-Type: application/x-www-form-urlencoded",
            "Cache-Control: no-cache",
            "Accept: application/json",
            "User-Agent: QianFanHttpClient/1.0.0"
        );
        $url = "{$this->domain}/openapi/{$path}";
        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_URL, $url);
        curl_setopt($curlHandle, CURLOPT_HEADER, 0);

        //much code here...

        $data = json_decode($response, true);
        if (json_last_error() != JSON_ERROR_NONE) {
            $error = json_last_error_msg();
            $errno = json_last_error();
            throw new Exception("Json Parsing Error #{$errno}. $error", 1);
        }
        return $data;
    }

    public function post($path, $data)
    {
        return $this->_request('POST', $path, $data);
    }
}

$abc = 'gg.com';
$efg = 'abc1234'
$client = new QF_HTTP_CLIENT();
$client -> domain = $abc;
$client -> token = $efg;

In my.php,

<?php
if(file_exists('class.php')){
    $qfappon = 1;
    required 'class.php';
}
if($qfappon == '1'){
    $rrr = $client->post(some of script here);
}
//my other script...
?>

My localhost, I delete class.php, doesn't fulfill the condition, so PHP ignore the $rrr = $clien......, and my localhost webpage running well.

But when I upload this my.php to my server without class.php also, my server webpage show 502 error. If I delete the $rrr = $client->post(some of script here); in my.php then my webpage will run as normal.

I feel like, at my server, even $qfappon is a blank value, doesn't match the condition, my server still process the script $rrr = $client->post(some of script here);, since $client->post class (missing) doesn't exist, so my webpage become 502 error.

Is it php version cause this? my server PHP 5.3.29, my XAMPP localhost 7.4.13

Swee Hong
  • 539
  • 2
  • 12
  • What happens if in your server, you var_dump($qfappon) before the `IF`? What is the output? – Andrea Olivato Jun 12 '21 at 06:03
  • Also, are you use that `file_exists('class.php')` returns false? Have you tried var_dump on that as well just to double check if there's an unexpected file with the same name? – Andrea Olivato Jun 12 '21 at 06:03
  • Why not a simple `if(file_exists('class.php')){..} else {...}`? – Michel Jun 12 '21 at 06:13
  • when I found this problem, I separate a folder and put this two file `my.php` and `class.php` for checking. when I `var_dump($qfappon)`, return `NULL`; – Swee Hong Jun 12 '21 at 06:13
  • @Michel , same, if `$client->post` script exist in `my.php` (doesn't matter match condition or not), the webpage will 502. If I delete `$client->post`, all running well. Buy at my localhost, above script doesn't cause any 502 error, running well, so I am very strange. – Swee Hong Jun 12 '21 at 06:21
  • Your development environment should be kept as similar as possible to the server environment, this can also cause it. What are you sending in the `$client->post(...)` ? What you send in could also cause the error. – prod3v3loper Jun 12 '21 at 08:55
  • At least you should define `$qfappon` before the first `if()`. If there is no `class.php`, in the second `if()` the variable is `undefined` and will throw a E_NOTICE. Also turn on [error_reporting](https://stackoverflow.com/q/1053424/1685196) so you can see what's happening. – Michel Jun 12 '21 at 09:30

0 Answers0