如何从REST php回应cURL响应

时间:2015-04-11 12:42:05

标签: php xml curl

我是关于PHP,REST和cURL的新手。我尝试构建一个应用程序来执行GET和POST请求。我做了一些教程并阅读了cURL文档。 我做了GET请求并获得了一些回复,但我无法得到我的实际需求:) 这是我的代码。

<?php
$url = 'https://ise.cisco.connect:9060/ers/config/portal';
$method = 'GET';

// headers and data (this is API dependent, some uses XML)
$headers = array(
'Accept: application/vnd.com.cisco.ise.identity.portal.2.0+xml'
);
$data = json_encode(array(
'firstName'=> 'John',
'lastName'=> 'Doe'
));
$username = 'sponsor';
$password = 'Password1';
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_USERPWD, $username.':'.$password);
curl_setopt($handle, CURLOPT_HEADER, 1);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);

switch($method)
{

case 'GET':
break;

case 'POST':
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'PUT':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
break;

case 'DELETE':
curl_setopt($handle, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}

$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
echo curl_getinfo($handle, CURLINFO_CONTENT_TYPE );


echo $code;
print_r ($response);


?>

CURLINFO_CONTENT_TYPE , CURLINFO_HTTP_CODE工作正常,但我也期待xml。

另外,

foreach( $response as $obj ):
            $output .= '<pre>' . htmlspecialchars( print_r($obj, true) ) . '</pre>';
        endforeach;

显示任何内容并以

的形式发出警告
Warning: Invalid argument supplied for foreach()

所有curl-getinfo功能都运行良好。如何获取我的实际xml数据?

那是print_r($response)输出

HTTP/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Expires: Thu, 01 Jan 1970 00:00:00 GMT Set-Cookie: JSESSIONIDSSO=55B8651BB47863C99C8B0450C61A84E2; Path=/; Secure; HttpOnly Set-Cookie: JSESSIONID=1BD1E6EB0CF6ED2D4CA6FCF019216431; Path=/ers/; Secure; HttpOnly Pragma: no-cache Date: Sat, 11 Apr 2015 13:24:11 GMT Content-Type: application/vnd.com.cisco.ise.ers.searchresult.1.0+xml;charset=utf-8 Content-Length: 1880 Server:

亲切的问候

0 个答案:

没有答案