PHP Curl POST,GET,PUT和DELETE不起作用

时间:2016-03-27 09:25:55

标签: php post curl get put

我有运行PHP 5.3的Centos 5.5机器。我正在尝试卷曲POST,GET,PUT和DELETE。他们都没有工作。我收到HTTP 401错误。

此处的实际文档: https://www.igniterealtime.org/projects/openfire/plugins/userservice/readme.html

这是POST信息:

Header: Authorization: Basic YWRtaW46MTIzNDU=
Header: Content-Type: application/xml

POST http://example.org:9090/plugins/userService/users

Payload Example 1 (required parameters):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<user>
 <username>test3</username>
 <password>p4ssword</password>
</user>

以下是PHP代码:

if ($method_name == 'POST')
{
$headers = array(
                    'Accept: application/xml',
        'Authorization: Basic '. $secret,
                    'Content-Type: application/xml'
    ); 
 print_r($headers);
 $body= <<<EOD
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <user>
  <username>testuser</username>
  <password>p4ssword</password>
  <name>Test User</name>
   <email>test@localhost.de</email>
   <properties>
    <property key="keyname" value="value1"/>
    <property key="anotherkey" value="value2"/>
  </properties>
  </user>
  EOD;
 $api_request_url .= '/users';
 echo "api_request_url=".   $api_request_url. "<BR>";
 curl_setopt($ch, CURLOPT_POST, TRUE);
 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body)); //$body
 print_r($body);
 //curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($api_request_parameters));
 }

 /*
 Here you can set the Response Content Type you prefer to get :
 application/json, application/xml, text/html, text/plain, etc
*/

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );  

/*
 Let's give the Request Url to Curl
*/
curl_setopt($ch, CURLOPT_URL, $api_request_url);

/*
 Yes we want to get the Response Header
(it will be mixed with the response body but we'll separate that after)
*/
curl_setopt($ch, CURLOPT_HEADER, TRUE);

/*
Allows Curl to connect to an API server through HTTPS
*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

/*
Let's get the Response !
*/
$api_response = curl_exec($ch);

/*
We need to get Curl infos for the header_size and the http_code
*/
$api_response_info = curl_getinfo($ch);

/*
Don't forget to close Curl
*/
curl_close($ch);

/*
 Here we separate the Response Header from the Response Body
*/
$api_response_header = trim(substr($api_response, 0,     $api_response_info['header_size']));
$api_response_body = substr($api_response, $api_response_info['header_size']);

// Response HTTP Status Code
echo $api_response_info['http_code']. "<BR>";

// Response Header
echo $api_response_header.  "<BR>";

0 个答案:

没有答案