Use curl to call interface data in thinkphp5.0

Hits: 0

//The first data calling method 
         $arr = array ( "a" => "123456" , "b123456" => "zhaotoubiao" );
          //Interface address and token 
         $url = 'https://address?token= token' ;
         $data_string=json_encode($arr);
         //initialization
         $ch = curl_init();

         //Set the post method to submit 
         curl_setopt($ch, CURLOPT_POST, 1 );
          //Set the fetched url
         curl_setopt($ch, CURLOPT_URL, $url);
         //set post data
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
         //Do not do certificate verification, deploy in linux environment and change to true 
         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false );
          //Set header file information as data stream output 
         curl_setopt($ch, CURLOPT_HTTPHEADER, array (
                  'Content-Type: application/json; charset=utf-8' ,
                  'Content-Length : ' . strlen($data_string))
         );
         ob_start();
          //Excuting an order
         curl_exec($ch);

         $return_content = ob_get_contents();
         ob_end_clean();
         $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         print_r($return_code);
         print_r($return_content);

Note: 1. The token in the address in the code

2. The data was empty when it was first made. Later, after adding these two lines of code, the ssl certificate verification data came out (I tested it locally), and it was opened normally on the server.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

Leave a Reply

Your email address will not be published.