ecshop请求restful的api

2015-03-24 10:27 来源:www.chinab4c.com 作者:ecshop专家

   最近需要使用ecshop请求外界的restful接口,我们知道ecshop程序是php的,所以这个时候我们只要使用ecshop调用php curl来完成ecshop对restful接口的请求。下面我们看以下代码。

if ($_GET['act'] == 'delete') {
    $curl_handle = curl_init ();
    curl_setopt ( $curl_handle, CURLOPT_URL, 'http://localhost:8080/v1/user/222');
    curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
    curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );
    curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
    curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
    curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
    curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
    curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, 'DELETE' );
    $ret = curl_exec ( $curl_handle );
    print_r($ret);
}
if ($_GET['act'] == 'put') {
     //PUT
    $curl_handle = curl_init ();
    // Set default options.
    curl_setopt ( $curl_handle, CURLOPT_URL, 'http://localhost:8080/v1/user/user_11111');
    curl_setopt ( $curl_handle, CURLOPT_FILETIME, true );
    curl_setopt ( $curl_handle, CURLOPT_FRESH_CONNECT, false );


    curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
    curl_setopt ( $curl_handle, CURLOPT_RETURNTRANSFER, true );
    curl_setopt ( $curl_handle, CURLOPT_TIMEOUT, 5184000 );
    curl_setopt ( $curl_handle, CURLOPT_CONNECTTIMEOUT, 120 );
    curl_setopt ( $curl_handle, CURLOPT_NOSIGNAL, true );
    curl_setopt ( $curl_handle, CURLOPT_HEADER, true );
    curl_setopt ( $curl_handle, CURLOPT_CUSTOMREQUEST, 'PUT' );
    $aHeader[] = "Content-Type:text/xml;charset=UTF-8";
    $aHeader[] = "x-bs-ad:private";
    curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $aHeader);
    $file = 'client.php';
    $file_size = filesize($file);
    $h = fopen($file,'r');
    curl_setopt ( $curl_handle, CURLOPT_INFILESIZE, $file_size);
    curl_setopt ( $curl_handle, CURLOPT_INFILE, $h);
    curl_setopt ( $curl_handle, CURLOPT_UPLOAD, true );
    $ret = curl_exec ( $curl_handle );
    print_r($ret);
}

来源:http://www.chinab4c.com