php - 使用 YII 框架发送 HTTP Post 请求

标签 php httpwebrequest yii http-post

有办法从我的 Controller 发送 HTTP post 请求吗? 我想发布数据,结果将返回到 JSON 中。 我没有找到关于 yii 的扩展和信息。

最佳答案

下面的代码应该适合你,只要确保启用 php_curl 扩展。

<?php

// URL on which we have to post data
$url = "http://localhost/tutorials/post.php";

// Any other field you might want to post
$json_data = json_encode(array("name"=>"PHP Rockstart", "age"=>29));
$post_data['json_data'] = $json_data;
$post_data['secure_hash'] = mktime();

// Initialize cURL
$ch = curl_init();

// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);

// Just for debug: to see response
echo $response;

关于php - 使用 YII 框架发送 HTTP Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10266104/

相关文章:

symfony - 持续部署和数据库迁移

mysql - 在 yii 中为 mysql 创建命令

php - 如何检查mysql中的参数是否为null或为空?

php - 正则表达式替换所有匹配项,除非匹配项位于字符串的末尾

php - for循环中的if语句只执行一次

.NET 网络爬虫需要测量 Time To First Byte of HttpWebResponse

asp.net - ASP.NET 如何检测浏览器功能?

php - 获取并更新表中所有行的列

c# - 使用 Fiddler 作为 HttpWebRequest 的代理需要运行 Fiddler

php - 时区 : Mysql save UTC and PHP read America/Sao_Paulo