search

Home  >  Q&A  >  body text

javascript - How to solve the error when PHP requests http post page?

Code:

<?php
/**
 * PHP发送Json对象数据
 *
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串
 * @return array
 */
function http_post_json($url, $jsonStr){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    $response = curl_exec($ch);//执行一个cURL会话,成功时返回tru,失败返回false
    $err = curl_error($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);//获取最后一次传输的相关信息,最后一个收到的HTTP代码

    curl_close($ch);
 
    return array($httpCode, $response, $err);
}

$time = time();

$url = "https://openapi.lechange.cn/openapi/accessToken";

$str = "phone:17691200000,time:{$time},nonce:49735441495760803893403522385895,appSecret:6d5c2c723dbb4ba78fac5a0e9ece81";

$str = md5($str);

$params = array(
            "system"=>array(
                'ver'=>'1.0',
                'sign'=>$str,
                'appid'=>'lcce1fdddaa5de4322',
                'time'=>"$time",
                'nonce'=>'49735441495760803893403522385895'
            ),
            "params"=>array("phone"=>"17691200000"),
            "id"=>"80"
        );

$jsonparams = json_encode($params);
echo $jsonparams;
list($returnCode, $returnContent, $err) = http_post_json($url, $jsonparams);

echo $returnCode;
var_dump($returnContent);
echo $err;
?>

During execution, two errors will be reported.
(1)SSL certificate problem: unable to get local issuer certificate. After adding the following two lines of code to the php.ini file, this error disappears.

curl.cainfo="D:/wamp64/ca-bundle.crt"
openssl.cafile="D:/wamp64/ca-bundle.crt"

(2) After the above error disappears,

appears
502 Bad Gateway
The proxy server received an invalid response from an upstream server.

There is really no other way now. . Even if the parameters or signature are wrong, the other server will return an error code, but this 502 does not even give the opportunity to return the error code. Ask God to help solve it.

PHP中文网PHP中文网2753 days ago513

reply all(5)I'll reply

  • 天蓬老师

    天蓬老师2017-05-16 13:18:17

    How come wamp server gets 502? 502 is a reverse proxy error.
    Is your local apache+php+mysql+windows running in reverse proxy mode or in apache2 handler mode?

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:18:17

    Obviously the other party’s website is down... When I open it directly in the browser, it also gets 502

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 13:18:17

    The prompt is more obvious. There is a problem with the backend service, so upstream returns a 502.

    The other party's backend service returned a result that the proxy did not accept when responding to your request (usually 4XX, 5XX).

    There are two situations:

    1. The other party’s backend service originally believed that there should be no 5XX results (or the other party’s backend service is down), so upstream does not support accepting such results. That is to ask the other party to change the service and not return a 5xx error.

    2. If the other party's backend service believes that it supports using 4xx/5xx to represent some results (common in Restful services), then the other party needs to modify the upstream configuration to support such results to be returned as normal results.

    reply
    0
  • PHPz

    PHPz2017-05-16 13:18:17

    Set : CURLOPT_SSL_VERIFYPEER => false

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:18:17

    Request page, interface debugging, use postman. A very useful tool. Simulate the request and you can easily get the request example code

    reply
    0
  • Cancelreply