How to make https post request in Cakephp

纵然是瞬间 提交于 2020-01-03 18:55:09

问题


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.


回答1:


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
?>



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/4254645/how-to-make-https-post-request-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!