记录了curl常用的get,post,post_file等功能的常用代码块

curl_get_http

  1. /**
  2. * curl get 请求
  3. * @param string $url 请求地址
  4. * @param array $data 请求数据
  5. * @param array $headers 请求header头数据
  6. * @return int|mixed
  7. */
  8. function curl_get_http($url, $data=[],$headers=[]){
  9. $curl = curl_init();
  10. if ($data){
  11. $url .= '?'.http_build_query($data);
  12. }
  13. curl_setopt($curl, CURLOPT_URL, $url);
  14. if ($headers) curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  15. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  16. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  17. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  18. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  19. $output = curl_exec($curl);
  20. if($output === false) {
  21. throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
  22. }
  23. curl_close($curl);
  24. return $output;
  25. }

curl_post_http

  1. /**
  2. * curl post 请求
  3. * @param string $url 请求地址
  4. * @param array $data 请求数据
  5. * @return int|mixed
  6. * @throws
  7. */
  8. function curl_post_http($url, $data){
  9. $curl = curl_init();
  10. curl_setopt($curl, CURLOPT_URL, $url);
  11. curl_setopt($curl, CURLOPT_POST, 1);
  12. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  13. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  15. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  16. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  17. $output = curl_exec($curl);
  18. if($output === false) {
  19. throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
  20. }
  21. curl_close($curl);
  22. return $output;
  23. }

curl_post_https

  1. /**
  2. * curl post 请求
  3. * @param string $url 请求地址
  4. * @param array $data 请求数据
  5. * @return int|mixed
  6. */
  7. function curl_post_https($url, $data){
  8. $curl = curl_init();
  9. curl_setopt($curl, CURLOPT_URL, $url);
  10. curl_setopt($curl, CURLOPT_POST, 1);
  11. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
  12. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  14. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);//严格校验
  15. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);//严格校验
  16. curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
  17. curl_setopt($curl, CURLOPT_SSLCERT, 'cert.crt');
  18. curl_setopt($curl, CURLOPT_SSLCERTPASSWD, '62090');
  19. curl_setopt($curl, CURLOPT_SSLKEYTYPE, 'PEM');
  20. curl_setopt($curl, CURLOPT_SSLKEY, 'rsa.key');
  21. $output = curl_exec($curl);
  22. if($output === false) {
  23. throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
  24. }
  25. curl_close($curl);
  26. return $output;
  27. }

curl_post_file_http

$data[‘file’] = uploadFile($_FILES[‘tmp_name’],$_FILES[‘type’],$_FILES[‘name’]);

  1. /**
  2. * 发送
  3. * @param string $url 请求地址
  4. * @param array $data 请求数据
  5. * @return int|mixed
  6. */
  7. function curl_post_file_http($url, $data)
  8. {
  9. $curl = curl_init();
  10. curl_setopt($curl, CURLOPT_URL, $url);
  11. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  12. curl_setopt($curl, CURLOPT_POST, 1);
  13. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  14. curl_setopt($curl, CURLOPT_TIMEOUT, 5);
  15. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  16. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  17. $output = curl_exec($curl);
  18. if ($output === false) {
  19. throw new Exception(sprintf("%s[%s]",curl_errno($curl), curl_error($curl)));
  20. }
  21. curl_close($ch);
  22. return $output;
  23. }
  24. //使用示例
  25. $curl_post_file_data['param'] = $param;
  26. $curl_post_file_data['guarantee_pdf'] = new CURLFile($local_file_path);//文件必须在本地
  27. $curl_post_file_ret = curl_post_file($url, $curl_post_file_data);

curl_post_files_http

  1. $data['files'] = [
  2. uploadFile($_FILES[0]['tmp_name'],$_FILES[0]['type'],$_FILES[0]['name']);
  3. uploadFile($_FILES[1]['tmp_name'],$_FILES[1]['type'],$_FILES[1]['name']);
  4. ]
  5. //请求方法用 curl_post_file($url, $data)

soap

  1. $soapClient = new \SoapClient(null,[
  2. 'location' => 'url',
  3. 'uri' => 'uri',
  4. ]);
  5. $ret = $soapClient->__soapCall('function_name',['param' => $param]);