php的两种post方式

<?php
$scheme = 'http';
$host = '127.0.0.1';
$path = '/oauth2/token.php';
$data = array('grant_type' => 'client_credentials');
$user_pass = 'testclient:testpass';
if( get_extension_funcs('curl') ){
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $scheme . '://' . $host . $path);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_USERPWD, $user_pass);
 curl_exec($ch);
 curl_close($ch);
}else{
 $data_str = http_build_query($data);
 $user_pass_encode = base64_encode($user_pass);
 $fp = @fsockopen($host, 80, $errno, $errstr, 30);
$header = "POST $path HTTP/1.1\r\n";
 $header .= "Host: 127.0.0.1\r\n";
 $header .= "Accept: */*\r\n";
 $header .= "Content-length: " . strlen($data_str) . "\r\n";
 $header .= "Authorization: Basic $user_pass_encode\r\n";
 $header .= "Content-type: application/x-www-form-urlencoded\r\n";
 $header .= "Connection: Close\r\n\r\n";
 $header .= $data_str . '\r\n\r\n';
if ($fp)
 {
 $line = '';
 fwrite($fp, $header);
 while (!feof($fp)) 
 {
 $line .= fgets($fp, 256);
 }
 fclose($fp);
echo $line;
 }
 else
 {
 echo $errno, " ", $errstr;
 }
}
此条目发表在 php 分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*


*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>