Dar
Dar

Reputation: 401

Send HTTP Post request with YII Framework

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.

Upvotes: 2

Views: 21615

Answers (4)

Madbreaks
Madbreaks

Reputation: 19549

yii-curl is another extension you can use, obviously a wrapper for PHP's cURL.

Upvotes: 1

Dar
Dar

Reputation: 401

I found solution for this: http://www.yiiframework.com/extension/ehttpclient/ This is yii extension from Zend Framework

Upvotes: 1

deej
deej

Reputation: 2564

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;

Upvotes: 4

user1233508
user1233508

Reputation:

You can do that using the curl PHP extension.

Upvotes: 0

Related Questions