用YII框架发送HTTP POST请求

问题描述:

有没有办法从我的Controller发送HTTP POST请求? 我想发布数据和结果将返回到JSON。 我没有找到有关yii的扩展和信息。用YII框架发送HTTP POST请求

我发现这个解决方案: http://www.yiiframework.com/extension/ehttpclient/ 这是Zend框架

您可以使用curl PHP扩展程序来做到这一点。

下面的代码应该适合你,只要确保启用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; 
+0

它很难做到这一点。如果我没有使用任何框架来制作HTTP请求,那就好了。我想找到方法,我可以使用yii框架来做到这一点。 – Dar 2012-04-22 12:39:04

yii-curl警予延长是另一个分机就可以使用,显然是PHP's cURL的包装。