Send HTTP Post request with YII Framework
19,916
Solution 1
I found solution for this: http://www.yiiframework.com/extension/ehttpclient/ This is yii extension from Zend Framework
Solution 2
Below code should work for you, just make sure to enable php_curl extension.
<?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;
Solution 3
yii-curl is another extension you can use, obviously a wrapper for PHP's cURL.
Author by
Dar
Updated on June 04, 2022Comments
-
Dar almost 2 years
There is way to send HTTP post request from my Controller? I want to post data and results would return into JSON. I didn't find extension and information for yii about that.
-
Dar almost 12 yearsIts hard way to do it. And its good if I don't use any framework to make HTTP Request. I want to find way I can use yii framework to do it.