I am currently developing an application in Laravel (v4.2.11) which is using ExtJS (v4.2.1-gpl).
As part of my ExtJS application, I am developing a JSON response that is used by ExtJS. However, I want to do the following thing:
return Response::json(array(
'menusystem' => array(
'listeners' => array(
'click' => function() {
location.href = 'test'
}
)
)
);
I do know this is not valid JSON. However, this is the way how the previous developer of the application did it that way. I'd like to know if this is possible within PHP, Laravel or JSON.
You can do something like this
$response = array(
'menusystem' => array(
'listeners' => array(
'click' => "%%%function() {
location.href = 'test'
}%%%"
)
)
);
$response = json_encode($response);
$response = str_replace('"%%%', '', $response);
$response = str_replace('%%%"', '', $response);
return $response;
It's only general conception. You can detect function structure in special macro etc.
Related
I am testing the CodeIgniter 3.1 code by making a simple web service.
I found some issue with the response of the API on IFTTT.
The code I have used for the API response in the CI Controller is as below
public function setup()
{
$method = $_SERVER['REQUEST_METHOD'];
$header = $this->input->request_headers();
$IFTTT_Channel_Key = $header["Ifttt-Channel-Key"];
$IFTTT_Service_Key = $header["Ifttt-Service-Key"];
if(
("xyz" === trim($IFTTT_Channel_Key) ) &&
("xyz" === $IFTTT_Service_Key)){
$data = array(
"data" => array(
"accessToken" => "1224124112411",
"samples" => array(
"actions" => array(
"turn_on_light" => array(
"device_name" => "my_device"
)
)
)
)
);
$ci =& get_instance();
$this->output->set_status_header(200);
$ci->output->set_content_type('application/json');
$ci->output->set_output(json_encode($data));
echo json_encode($data);
}
}
The response in the IFTTT Platform :
���{"data":{"accessToken":"1224124112411","samples":{"actions":{"turn_on_light":{"device_name":"my_device"}}}}}
The Response in the Postman tool :
{"data":{"accessToken":"1224124112411","samples":{"actions":{"turn_on_light":{"device_name":"my_device"}}}}}
Note: Please don't edit the question without understanding the actual issue. Please
I am trying to get search results as per the api documentation
Here is what I wrote in PHP
require_once 'HTTP/Request2.php';
$api_key = 'my_bing_search_api_key';
$url_encoded_keyword = urlencode('animation concepts and tutorials');
$request = new \Http_Request2('https://api.cognitive.microsoft.com/bing/v5.0/search');
$headers = [
'Ocp-Apim-Subscription-Key' => $api_key
];
$request->setHeader($headers);
$url = $request->getUrl();
$parameters = [
'q' => $url_encoded_keyword,
'count' => '10',
'offset' => '0',
'safesearch' => 'Strict',
);
$url->setQueryVariables($parameters);
$request->setMethod(\HTTP_Request2::METHOD_GET);
$request->setBody("{body}");
$search_result = null;
try {
$response = $request->send();
$search_results = json_decode($response->getBody(), true);
return $search_results;
} catch (HttpException $ex) {
return [];
}
I am getting response but it is not having webPages property. It has _type, rankingResponse, relatedSearches and videos properties only.
I tested the same request in the api console. There I am getting the webPages property in the json response.
Any ideas what could have been the reason why I am not getting the webPages in PHP but works on microsoft's api tester site?
From the code snippet, you are passing the keyword to Bing web search API after encoding it.
$url_encoded_keyword = urlencode('animation concepts and tutorials');
$parameters = [
'q' => $url_encoded_keyword,
'count' => '10',
'offset' => '0',
'safesearch' => 'Strict',
);
Try without encoding the keyword. From their API testing console, HTTP request for the same keyword would appear as
https://api.cognitive.microsoft.com/bing/v5.0/search?q=animation
concepts and tutorials&count=10&offset=0&mkt=en-us&safesearch=Moderate
I'm trying to access a webservice. I'm already using this webservice from an android app but now I need to access some functions from a php document. If I use chromes advanced rest client application for testing it works fine if I select the POST option and application/x-www-form-urlencode as content-type. But when I try to access the webservice from my PHP file I get the response from the server that it can't find the value "tag". This is the code:
$data = array( 'tag' => 'something');
$options = array('http' => array(
'method' => 'POST',
'content' => $data,
'header' => "Content-Type: application/x-www-form-urlencode")
);
$context = stream_context_create($options);
$url = 'myurl';
$result = file_get_contents($url,false,$context);
$response = json_decode($result);
What is wrong with this code?
Thanks for any help!
Try this:
$data = http_build_query( array( 'tag' => 'something') );
As defined here, "Content" value must be a string: http_build_query generate the URL-encoded query string you need.
I'm a newbie in web development (or development) and I'm using Laravel (if it matters somehow). I have a reCAPTCHA in my form. It works, but now I have to verify user's keyword in recaptcha.
Documentation: https://developers.google.com/recaptcha/docs/verify sounds simple, but somehow I can't write the code lines for verification. I was looking for some examples, but they're not for Laravel but for pure PHP, and I didn't see where is the 'https://www.google.com/recaptcha/api/siteverify?secret=your_secret&response=response_string&remoteip=user_ip_address' URL used in the examples.
My controller is:
public function postMessage() {
require_once('recaptchalib.php');
$privatekey = "MY_PRIVATE_KEY";
?????
// this should be executed only after validation
DB::table('messages')->insert(['name' => Input::get('name'),
'email' => Input::get('email'),
'message' => Input::get('message'),
'datetime' => \Carbon\Carbon::now()->toDateTimeString()]);
So please, instruct me, how could i get JSON object with response, or what should I write instead of ?????
Well, at the end I was able to find solution by myself:
public function postMessage() {
$secret = '7LdL3_4SBAAAAKe5iE-RWUya-nD6ZT7_NnsjjOzc';
$response = Input::get('g-recaptcha-response');
$url = 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$response;
$jsonObj = file_get_contents($url);
$json = json_decode($jsonObj, true);
if ($json['success']==true) {
DB::table('messages')->insert(['name' => Input::get('name'),
'email' => Input::get('email'),
'message' => Input::get('message'),
'datetime' => \Carbon\Carbon::now()->toDateTimeString()]);
return Redirect::route('contact')->with('okmessage', 'Ďakujeme. Vaša správa bola odoslaná');
} else {
return Redirect::route('contact')->with('errormessage', 'Chyba! Zaškrtnite "Nie som robot" a zadajte správny kód.');
}
}
I have a service that expects 2 objects... Authentication and Client.
Both are mapped correctly.
I am trying to consume them as Json, but I'm having a hard time doing that.
If I specify just one parameter it works fine, but how can I call this service passing 2 parameters? Always give me some exception.
Here is my rest service:
#POST
#Path("login")
#Consumes("application/json")
public void login(Authentication auth, Client c)
{
// doing something
}
And here is my PHP consumer:
$post[] = $authentication->toJson();
$post[] = $client->toJson();
$resp = curl_post("http://localhost:8080/login", array(),
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => $post));
I tried some variations on what to put on CURLOPT_POSTFIELDS too but couldn't get it to work.
The problem you are probably having, is that you are declaring $post as a numbered array, which probably contains the array keys you are mapping. Basically, this is what you are giving it:
Array(
1 => Array(
'authentication' => 'some data here'
),
2 => Array(
'client' => 'some more data here'
)
)
When in reality, you should be creating the $post var like so:
Array(
'authentication' => 'some data here',
'client' => 'some more data here'
)
Try changing your code to something more like this (not optimal, but should get the job done):
$authentication = $authentication->toJson();
$client = $client->toJson();
$post = array_merge($authentication, $client);
$resp = curl_post("http://localhost:8080/login", array(),
array(CURLOPT_HTTPHEADER => array('Content-Type: application/json'),
CURLOPT_POSTFIELDS => $post));