This question is related to the CodeIGniter RESTful API library found here. I hope someone here is using this library and can offer some help :) .
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
I have installed this library and set up a working environment with it. I have been able to get data back as well. For use, I have created a very simple class that I am accessing with the following code:
<?php
require APPPATH.'/libraries/REST_Controller.php';
class Users extends REST_Controller
{
public function list_get()
{
$this->load->database();
$data = $this->db->get('users')->result();
$this->response($data, 200);
}
}
To get at this controller, I have done a call to the following URL:
"http://localhost/mgtapp/index.php/api/users/list/format/json"
While I am getting back data, I see that the header content type is set to text/html instead of json and I am also getting errors in php that say "headers already sent". I have tried to remove the format from the end of the url and send it in via an "accept" but I get the same errors and I see the content type being set as text/html. When I run the example, I see the response coming back as it should (as json in the content type) so I do not understand what I am doing wrong here that the content type is not being set correctly. IF someone can shed some light it would be super helpful!
Thanks!
So I am answering this question because the reason for my troubles was a stupid one.
I had a couple of lines under the "?>" of my php class controller and apparently that was messing it up. Sorry!
Anybody else with this issue, check that first!
Related
Im Lost Please, How do I resolve the following on CodeIgniter using
REST-cliente library (REST).
I try send POST, the webApp or WebResourse is based on (ASP.net), so the idea is this:
getUser()
function getUser($ID){
//set config of the URL
$config = array('server' => 'http://www.example.com/app/webapp/);
//initialize
$this->rest->initialize($config);
//now here is my problem look at this Im lost, I try this but I dont understand:
$postData = '__VIEWSTATE=/DKEKCdmdaEKEKEcnaEKEqkencKJE&'.
'__EVENTVALIDATE=/DWRRKCkadjeEWWrjrDSJEJeW&'.
'buttonSearch=search&'.
'textFieldUserID='.$ID.';
$this->rest->option(CURLOPT_POSTFIELDS, $postData);
$this->rest->post('query/user.apsx');
//When run this, I use debug but NO show ERROR, this only return content html of page whitout query of user data.
$this->rest->debug();
Note: I user library rest and curl is named within constructor. I use this for request GET and work perfectly. but I want to do POST
$this->load->library('curl');
$this->load->library('rest');
The library:
https://github.com/philsturgeon/codeigniter-restclient
Thanks.!!!
As you stated in your question:
//When run this, I use debug but NO show ERROR, this only return
content html of page whitout query of user data.
$this->rest->debug();
So, if you received HTML but not the desired data, the problem is the server you are asking to. It's more than sure that you're not doing the right request, but is weird because they should show you what is wrong.
Other thing would be, could you paste the given answer? Just to know what are the errors, if any.
I am having troubles with my PHP application which uses Drive SDK. I am trying to update a file, but all the time I receive 500 Internal Error message when I try to update file's contents.
I am looking for some way to debug the application. What would be most helpful for me is possibility to view how the entire request along with all headers look like. Is there any way to check it, or are there any other options for debugging?
Thank you a lot for your time.
I still didn't find any option for debugging- However, I found how I can view the requests done by the API client.
Open google-api-php-client/io/Google_REST.php file and find static public function execute(Google_HttpRequest $req) function.
There you will find this line:
$httpRequest = Google_Client::$io->makeRequest($req);
Right under it put the following code: var_dump($httpRequest);
During every request the client will do, you will get dump of it's request.
This question is still relevant but the accepted answer is very old. If you wish to view the HTTP requests and server responses in version three, the file you need to edit is /vendor/google/apiclient/src/Google/Http/REST.php. Locate the doExecute function and add print_r($request->getUri()); to the first line. Add print_r($response->getBody()->read(1024)); to the line just before the function returns to see the response body.
Like in the title. I have a problem with my own custom token implemented via hook_civicrm_tokens and also hook_civicrm_tokenValues. When I try to send an email manualy, the token appears correctly in the list and it also appers parsed in the mail that the client recevies, the same with message templates expect that it appears as not parsed like {mycat.token}. It puzzles me because other tokens are inserted correctly in any case.
The code I've implemented looks something like this
function modulname_civicrm_tokens(&$tokens){
$tokens['mytoken'] = array('mytoken.something' => 'token name');
}
function modulename_civicrm_tokenValues(&$values, $contactIDs){
// some fancy logic for extracting data
foreach ($contactIDs as $cid){
$values[$cid]['mytoken.something'] = 'some data from other function connected with cid';
}
}
Sorry if I messed up the code block.
Have enyone encountered this thing? Any tips how to fix this?
Ok, I found out why this did not work. Somebody did a hack in a module that only used the template body from civicrm and send the message via mimemail so no wonder that it did not work .
I am trying to enable CORS for an API built in CakePHP so that all requests are accessible with the following in the AppController:
public function beforeFilter()
{
header("Access-Control-Allow-Origin: *");
}
Is this in the wrong place? As requests are still being blocked.
Update: It seems this does in fact work BUT because I am doing something like:
header('Content-Type: application/json');
echo json_encode(array('message'=>'Hello world!'));
In some of my methods it's acting as though it's overriding the header set the AppController so it's not appearing in the response for the JSON calls.
Any ideas?
Update 2: Returning JSON like below, fixes the problem:
$this->response->type('json');
$this->response->body(json_encode(array('message'=>'Hello world!')));
So apparently using header() in Cake breaks previous headers?
You can do this using the cake response object;
$this->response->header('Access-Control-Allow-Origin', '*');
More info on the response object;
http://book.cakephp.org/2.0/en/controllers/request-response.html#setting-headers
However, the beforeRender() callback seems a more logical location.
Another option is to add this header in your apache vhost or htaccess examples can be found in the htaccess file of Html5Boilerplate which is a very interesting thing to look at (well documented), because it contains a lot of optimisations that work nicely with cakephp as well;
https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess
http://html5boilerplate.com/
Based on what I found out here: Sending correct JSON content type for CakePHP
The correct way to return JSON in CakePHP is like so:
$this->response->type('json');
$this->response->body(json_encode(array('message'=>'Hello world!')));
This is because the headers can be overridden and therefore the CORS doesn't work unless you do it the 'proper' way using the response object in Cake.
How can I implement jquery in my Zend Framework application in a custom manner.
appending jquery.js ok
appending script ok
send POST data to controller ok
process POSTed data ok
send 'AjaxContext' respond to client now ok (thanks)
I'm using jquery for the first time, what am I doing wrong?
Early on, the best practice to get Zend to respond to ajax requests without the full layout was to check a variable made available via request headers. According to the documentation many client side libraries including jQuery, Prototype, Yahoo UI, MockiKit all send the the right header for this to work.
if($this->_request->isXmlHttpRequest())
{
//The request was made with via ajax
}
However, modern practice, and what you're likely looking for, is now to use one of two new helpers:
ContextSwitcher
AjaxContent
Which make the process considerably more elegant.
class CommentController extends Zend_Controller_Action
{
public function init()
{
$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('view', 'html')
->initContext();
}
public function viewAction()
{
// Pull a single comment to view.
// When AjaxContext detected, uses the comment/view.ajax.phtml
// view script.
}
Please Note: This modern approach requires that you request a format in order for the context to be triggered. It's not made very obvious in the documentation and is somewhat confusing when you end up just getting strange results in the browser.
/url/path?format=html
Hopefully there's a workaround we can discover. Check out the full documentation for more details.
Make sure your using $(document).ready() for any jQuery events that touch the DOM. Also, check the javascript/parser error console. In Firefox it's located in Tools->Error Console. And if you don't already have it installed, I would highly recommend Firebug.
This should have been a comment, can't, yet...
It has nothing to do with ZF+Jquery combination.
First try a proto of what you need with a simple php file. No framework, just Jquery and straight forward, dirty php.
Oh, and don't forget to track what happens with FireBug.