有些东西总会像乌云一样笼罩着你的生活, 尽管, 你想忽视它, 你想离开他, 但是, 他总是不时地展现出对你的生活的影响力. 这里面就有scp和curl. 但是, 我猜到了开头, 没有猜到结局.

scp 开胃小菜

scp是文件传输, 主要针对sftp, 这个是ssh的文件传输协议, 类似ftp, 但是会比较安全.

curl

好吧, 一开始, 我以为这货和我以前碰到的linux命令差不多, 或者最多想git命令吧.

实际情况是, 这是一只看起来像是羊的哥斯拉, 有兴趣的同学可以__curl —help__一下, 立刻可以看到一个壮观至极的景象. 参数介绍滚滚而上, 仿佛尼亚加拉大瀑布从面前涌过.

我们看一眼 curl —manual, 再次感受了瀑布, 弄一点开头介绍一下吧:

NAME
       curl - transfer a URL

SYNOPSIS
       curl [options] [URL...]

DESCRIPTION
       curl  is  a tool to transfer data from or to a server, using one of the
       supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS,  IMAP,
       IMAPS,  LDAP,  LDAPS,  POP3,  POP3S,  RTMP, RTSP, SCP, SFTP, SMB, SMBS,
       SMTP, SMTPS, TELNET and TFTP). The command is designed to work  without
       user interaction.
# curl是一个工具, 传输数据的. 设计目标是不需要用户交互操作.
       curl offers a busload of useful tricks like proxy support, user authen-
       tication, FTP upload, HTTP post, SSL connections, cookies, file  trans-
       fer  resume,  Metalink,  and more. As you will see below, the number of
       features will make your head spin!
#基本上能想到的, 他都支持, 代理, ssl, ftp, http, 断点续传, cookies.
       curl is powered by  libcurl  for  all  transfer-related  features.  See
       libcurl(3) for details.
#他背后有个库, 支持所有传输相关的特性(features).
URL
       The  URL  syntax is protocol-dependent. You'll find a detailed descrip-
       tion in RFC 3986.
#url遵守协议规定.
       You can specify multiple URLs or parts of URLs  by  writing  part  sets
       within braces as in:

         http://site.{one,two,three}.com
#你可以这样写url, 不知道你们怎么想, 我已经震惊了.
       or you can get sequences of alphanumeric series by using [] as in:

         ftp://ftp.numericals.com/file[1-100].txt
#又惊了一次.
         ftp://ftp.numericals.com/file[001-100].txt    (with leading zeros)

         ftp://ftp.letters.com/file[a-z].txt
         
         Nested  sequences  are not supported, but you can use several ones next
       to each other:

         http://any.org/archive[1996-1999]/vol[1-4]/part{a,b,c}.html
#不知道你们这么想, 反正我的脑子已经抽了.
#阮一峰有个举例: http://www.ruanyifeng.com/blog/2011/09/curl.html

php, curl 手牵手

那么, 问题来了, curl这么牛, php知道么?

php有函数库. 貌似mac下面是不需要安装的.

	$url = 'https://oauth.taobao.com/authorize';
	
	#淘宝需要这么搞参数.
	$postfields= array('grant_type'=>'authorization_code',
	'client_id'=>$appkey, //这个地方appkey
	'client_secret'=>$secret, //这个地方secret
	'code'=>'test', //这个地方是前面传过来的code
	'redirect_uri'=>'http://test.hujing888.com', //这个是回调地址.
	'response_type'=>'code'	//不指定这个会报错.
	); 
	$post_data = ''; 
	foreach($postfields as $key=>$value){
		$post_data .="$key=".urlencode($value)."&";
	}
	#一直到这里, 都在搞参数
	
	// 1. 初始化
	$ch = curl_init();
	// 2. 设置选项,包括URL, 除了url, 其他的有可能都没啥用.
	curl_setopt($ch,CURLOPT_HEADER,0);
	curl_setopt($ch, CURLOPT_URL, $url); //这个肯定有用.
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
	//指定post数据
	curl_setopt($ch, CURLOPT_POST, true);

	//添加参数, 之前搞得参数, 就用在这里了.
	curl_setopt($ch, CURLOPT_POSTFIELDS, substr($post_data,0,-1));
	// 3. 执行并获取HTML文档内容	 
	$output = curl_exec($ch); // 这句最关键, 执行了这个调用, 然后, 后面的东西其实没啥用了, 因为根本就不会执行了---或许不执行, 待验证.
	
	$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
	if($output === FALSE ){
		echo "CURL Error:".curl_error($ch);
	}
	echo $httpStatusCode;
	 
	// 4. 释放curl句柄	
		
	curl_close($ch);
	var_dump($output);

简洁的写法:

#淘宝官方的curlopt_postfields的写法太缠绕了.