百度搜索API v3版本与soap
技术文档请参考http://dev2.baidu.com/docs.do?product=2#page=File,和http://dev2.baidu.com/docs.do?product=2#page=file1,http://dev2.baidu.com/docs.do?product=2#page=Request_Header
因为在每次访问API时,您需要提供您的token(权限代码)以及要操作的用户名和密码来进行权限认证。
那么如何验证?请看下面的代码:
class Baidu_api { //soap连接标识 private $soap_client; public function __construct($service_name) { $this->soap_client = new SoapClient ( URL . '/sem/sms/v3/' . $service_name . '?wsdl', array ('trace' => TRUE, 'encoding' => 'utf-8') ); //帐号信息 $sh_param = array ('username' => USERNAME, 'password' => PASSWORD, 'token' => TOKEN ); $headers = new SoapHeader ( 'http://api.baidu.com/sem/common/v2', 'AuthHeader', $sh_param ); $this->soap_client->__setSoapHeaders ( array ( $headers ) ); } //取得公开的函数 public function get_functions() { return $this->soap_client->__getFunctions(); } //取得公开函数的参数 public function get_types() { return $this->soap_client->__getTypes(); } //执行函数接口 public function soap_call($function_name, array $arguments, array &$output_headers) { return $this->soap_client->__soapCall($function_name, $arguments, NULL, NULL, $output_headers); } public function get_last_request() { return $this->soap_client->__getLastRequest(); } public function get_last_response() { return $this->soap_client->__getLastResponse(); } }
construct部份就是验证。
如何请求对应的接口,举个例子:在reportservice中的接口getProfessionalReportId
function get_professional_report_id($config) { $param = array( 'getProfessionalReportIdRequest' => array( 'reportRequestType' => $config ) ); $output = array(); $output_response = $this->client->soap_call('getProfessionalReportId', $param, $output); //保存记录 $this->baidu_log_mdl->write_log('getProfessionalReportId||' . serialize($output)); if(isset($output_response->reportId)) return $output_response->reportId; return FALSE; }