PHP cURL-curl_setopt的奇怪问题

时间:2018-09-03 18:30:34

标签: php curl

我有一个非常奇怪的问题,这是我的代码:

function myFunction($id, $name, $secret) {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, 'https://www.myurl.com/api');
        curl_setopt($ch, CURLOPT_POST, 1);

        curl_setopt($ch, CURLOPT_POSTFIELDS, 
                 http_build_query(
                    array(
                        'Client_id'     => $id,
                        'Client_secret' => $secret, // $secret = Ve6UZ0cox=ry?2F9>qegmB:NCh?EQS?]cKmkjeHjS=3t1=E<RJ
                        'Client_name'   => $name
                    )));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $results = curl_exec($ch);
        $results = json_decode($results, true);

        print_r($results);

        curl_close ($ch);

        // curl_close($curl);
        return $results['Token_type'].' '.$results['Access_token'];
    }

上面的代码不起作用,可能是由于$ secret中的特殊字符所致。 但是,当我直接编写$ secret值时,它可以工作!

curl_setopt($ch, CURLOPT_POSTFIELDS, 
                     http_build_query(
                        array(
                            'Client_id'     => $id,

                            // Now, it works!
                            'Client_secret' => 'Ve6UZ0cox=ry?2F9>qegmB:NCh?EQS?]cKmkjeHjS=3t1=E<RJ'
                            'Client_name'   => $name
                        )));

结论:

'Client_secret' => $secret // Doesn't work
'Client_secret' => 'Ve6UZ0cox=ry?2F9>qegmB:NCh?EQS?]cKmkjeHjS=3t1=E<RJ' // Work!

是的,我尝试了这个操作:die($secret);,它也显示Ve6UZ0cox=ry?2F9>qegmB:NCh?EQS?]cKmkjeHjS=3t1=E<RJ

你能做什么?感谢您的帮助。 ;-)

干杯, 塞巴

0 个答案:

没有答案