如何在Cakephp中进行https发布请求

时间:2010-11-23 09:43:57

标签: php rest cakephp post https

我要求app必须通过HTTPS POST进行REST API调用。我是cakephp的新手。我在想是否可以使用httpsocket进行https调用。

我感谢任何帮助。

感谢。

3 个答案:

答案 0 :(得分:23)

您可以使用其中任何一种

CAKEPHP SOCKET

// Use either of the following two:
App::import('Core', 'HttpSocket'); // Cake 1.x
App::uses('HttpSocket', 'Network/Http'); // Cake 2.x

$HttpSocket = new HttpSocket();
$results = $HttpSocket->post('www.somesite.com/add', array('name' => 'test', 'type' => 'user'));  
//$results contains what is returned from the post.

CURL

$url = 'http://domain.com/get-post.php';
$fields = 'var1=value1&var2=value2';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
$result = curl_exec($ch);
curl_close($ch);

JAVASCRIPT如果您希望在客户端完成此操作

答案 1 :(得分:3)

App::import('Core', 'HttpSocket');

在Cake 2.x上对我不起作用,但是

App::uses('HttpSocket', 'Network/Http');

确实有效。这里有更多关于HttpSocket http://book.cakephp.org/2.0/en/core-utility-libraries/httpsocket.html

的信息

答案 2 :(得分:2)

如果您启用了PHP的Curl模块:

<?php
// create a new cURL resource
$ch = curl_init();

$data = array('Your' => 'Data');

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// grab URL and pass it to the browser
$result = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

print_r($result); // output result for all the kings
?>