如何在 Cakephp 中发出 https post 请求

发布于 2024-10-04 11:03:48 字数 127 浏览 1 评论 0原文

我有一个要求,应用程序必须通过 HTTPS POST 进行 REST API 调用。我是 cakephp 的新手。我在想是否可以使用 httpsocket 进行 https 调用。

我很感激任何帮助。

谢谢。

I have requirement where app has to make REST API calls over HTTPS POST. I am new to cakephp. I was thinking if I could do https calls using httpsocket.

I appreciate any help.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柠檬色的秋千 2024-10-11 11:03:48

您可以使用其中任何一个

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 JAVASCRIPT

$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);

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

You can use any of these

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 if you want this to done at client side

我是男神闪亮亮 2024-10-11 11:03:48

如果您启用了 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
?>

If you have PHP's Curl module enabled:

<?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
?>
怀中猫帐中妖 2024-10-11 11:03:48
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

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

did not work for me on Cake 2.x, but

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

did work. Here's more about HttpSocket http://book.cakephp.org/2.0/en/core-utility-libraries/httpsocket.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文