有办法从我的控制器发送 HTTP 发布请求吗?我想发布数据,结果将返回 JSON。我没有找到有关 yii 的扩展名和信息。
问问题
19929 次
4 回答
3
下面的代码应该适合你,只要确保启用 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;
于 2012-04-22T08:53:13.810 回答
1
我找到了解决方案: http ://www.yiiframework.com/extension/ehttpclient/ 这是 Zend Framework 的 yii 扩展
于 2012-04-23T05:34:59.977 回答
1
yii-curl是您可以使用的另一个扩展,显然是PHP 的 cURL的包装器。
于 2013-04-02T17:17:04.890 回答
0
你可以使用curl PHP 扩展来做到这一点。
于 2012-04-22T07:42:05.750 回答