昨天做个小东西,要访问别人的接口,发现直接用psot方式访问不能取得数据。
来段直接post的代码吧。
$url = "http://www.jinsong.com/index/index1"; $post_data = array ("keywords" => $keywords,"title" => $title); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 注意简单post不要加这个 $result = curl_exec($ch); $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); //这里是看返回状态代码的 print_r($result); var_dump($httpCode); curl_close($ch);
具体不解释了,反正百度一百度一大堆。
用这种方式返回不了数据。估计别人后台接口限定了只能ajax访问了。加个头文件就行了。
下面的代码其实原理都是一样的,只是多了头文件,再就是把数组转成json提交了。
$api = "http://www.jinsong.com/index/index1"; $post_data = array ("keywords" => $keywords,"title" => $title); $post_data = json_encode($post_data); $ch = curl_init(); $options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => $post_data, CURLOPT_HTTPHEADER => array("Content-Type:application/json; charset=utf-8","X-Requested-With: XMLHttpRequest","Content-Length: ".strlen($post_data)), CURLOPT_USERAGENT =>'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11' ); curl_setopt_array($ch, $options); $result = curl_exec($ch); $httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE); //这里是看返回状态代码的 curl_close($ch); print_r($result);
注意重点在
X-Requested-With: XMLHttpRequest
既然post写了,干脆上个简单的get例子吧,也算curl的使用方法大全了
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://www.jinsong.com/index/index1?&keywords=".$keywords."&title=".$title); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); print_r($output);
别人封装的一个函数库也放上来
/** * curl 函数 * @param string $url 请求的地址 * @param string $type POST/GET/post/get * @param array $data 要传输的数据 * @param string $err_msg 可选的错误信息(引用传递) * @param int $timeout 超时时间 * @param array 证书信息 * @author liw@alltosun.com **/ public function go_curl($url, $type, $data = false, &$err_msg = null, $timeout = 20, $cert_info = array()) { $type = strtoupper($type); if ($type == 'GET' && is_array($data)) { $data = http_build_query($data); } $option = array(); if ( $type == 'POST' ) { $option[CURLOPT_POST] = 1; } if ($data) { if ($type == 'POST') { $option[CURLOPT_POSTFIELDS] = $data; } elseif ($type == 'GET') { $url = strpos($url, '?') !== false ? $url.'&'.$data : $url.'?'.$data; } } $option[CURLOPT_URL] = $url; $option[CURLOPT_FOLLOWLOCATION] = TRUE; $option[CURLOPT_MAXREDIRS] = 4; $option[CURLOPT_RETURNTRANSFER] = TRUE; $option[CURLOPT_TIMEOUT] = $timeout; //设置证书信息 if(!empty($cert_info) && !empty($cert_info['cert_file'])) { $option[CURLOPT_SSLCERT] = $cert_info['cert_file']; $option[CURLOPT_SSLCERTPASSWD] = $cert_info['cert_pass']; $option[CURLOPT_SSLCERTTYPE] = $cert_info['cert_type']; } //设置CA if(!empty($cert_info['ca_file'])) { // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO $option[CURLOPT_SSL_VERIFYPEER] = 1; $option[CURLOPT_CAINFO] = $cert_info['ca_file']; } else { // 对认证证书来源的检查,0表示阻止对证书的合法性的检查。1需要设置CURLOPT_CAINFO $option[CURLOPT_SSL_VERIFYPEER] = 0; } $ch = curl_init(); curl_setopt_array($ch, $option); $response = curl_exec($ch); $curl_no = curl_errno($ch); $curl_err = curl_error($ch); curl_close($ch); // error_log if($curl_no > 0) { if($err_msg !== null) { $err_msg = '('.$curl_no.')'.$curl_err; } } return $response; }