when I use dd($arr) in controller the decoded json data gets printed correctly. How can I pass this decoded json data into my view?
Controller Code
public function test()
{
// $sld = $request['sld'];
// $tld = $request['tld'];
$response = Curl::to('https://reseller.enom.com/interface.asp?command=check&sld=decksys&tld=info&responsetype=xml&uid=resellid&pw=resellpw')
->get();
//check if we are getting a response
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json, true);
dd($arr);
return view('clientlayout.main.test', compact('arr'));
}
Blade File:
`{{ $arr }}`
My Route file is given below:
Route::get('/test','EnomController#test');
How to solve this error?
htmlspecialchars() expects parameter 1 to be string, array given
return view('clientlayout.main.test', compact('arr'))->with('arr', $arr);
and your view: {{ $data['obj'] }} will work.
The answer for this error is already available on stack, you are accessing those objects which are empty or blank here is a link of solution:
htmlspecialchars expects parameter 1 to be string
ok in controller use curl like:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,
"https://reseller.enom.com/interface.asp?command=check&sld=decksys&tld=info&responsetype=json&uid=resellid&pw=resellpw");
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$arr = json_decode(curl_exec($ch), true);
curl_close($ch);
then return data to view, use foreach loop in view like:
#foreach($arr as $item)
{{$item['Command']}}
{{$item['APIType']}}
#endforeach
and want to check before print use isset:
#if(isset($item['Command']))
{{$item['Command']}}
#endif
Hope this help
Related
How can I send data from controller to external api in Laravel?
public function syncData(Request $request)
{
$datas = Data::all();
$newDatas = NewData::all();
$url = 'mydomain.com/api/sync-data';
}
I want to send $datas and $newDatas to $url through Laravel controller and perform some actions on those data. How can I achieve that?
You could use the Laravel HTTP Client (which is a wrapper for Guzzle HTTP Client) to perform a request to that API.
use Illuminate\Support\Facades\Http;
$response = Http::post('mydomain.com/api/sync-data', [
'data' => Data::all(),
'newdata' => NewData::all(),
]);
public function syncData(Request $request)
{
$datas = Data::all();
$newDatas = NewData::all();
$url = 'mydomain.com/api/sync-data';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datas));
$output = curl_exec($ch);
curl_close($ch);
}
let me know if it is helpful.
important things to notice here.
you have to know what your external API type is. is it POST or in GET method.
my example above is just a sample code to make you understand how you will use curl it is not tested in regard to your context
sendsms( '919918xxxxxx', 'test message from spring edge' ); how do you declare this function in codeigniter?
Codeigniter have a good functionality Helpers. Helper functions are available in all controllers(if auto-load). You can use codeigniter helper to declare this function.
Open a file in Application/Heplers/sendsms_helper.php
/*Start sendsms_helper.php file */
function sendsms($number, $message_body, $return = '0'){
$sender = 'SEDEMO'; //Use sender name here
$smsGatewayUrl = 'SMS_GATEWAY_URL_HERE';
$apikey = '62q3z3hs49xxxxxx'; // Use API key here
$textmessage = urlencode($textmessage);
$api_element = '/api/web/send/';
$api_params = $api_element.'?apikey='.$apikey.'&sender='.$sender.'&to='.$mobileno.
'&message='.$textmessage;
$smsgatewaydata = $smsGatewayUrl.$api_params;
$url = $smsgatewaydata;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
if($return == '1'){
return $output;
}
}
/* * End sendsms_helper.php file */
How to use in controller:
Load sendsms helper in controller using $this->load->helper('sendsms_helper');
Call sendsms function Ex. sendsms( '919918xxxxxx', 'test message from spring edge' );
I have a Post Route in Laravel, so if a user post data to the route I want to add data to request then posting it to another post route that I have then show the user the result that I get from the second route.
Is there way to do it? or I should use GuzzleHttp?
You can use curl to perform the sub request.
Route::any('/proxy', function (Request $request) {
//Get all of the input from the request and store them in $data.
$data = $request->all();
//initialise your other data.
$data2 = ['xyz' => "xyz"];
$newPayload = array_merge($data, $data2);
$endPoint = "https://example.org";
$ch = curl_init($endPoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $newPayload);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
return (string)$response;
});
I used the following function to get google search images
function get_images($query){
$url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=';
$url .= urlencode($query);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
//decoding request
$result = json_decode($data, true);
return $result;
}
It works good when I output all results using print_r()
$images = get_images("porsche");
print_r($images);
But when I want to get a specific value using foreach()
foreach ($images->responseData->results as $result) {
echo $result->url;
}
I got an error:
Notice: Trying to get property of non-object
How can I get any specific value separetly using foreach()?
Try this :
<?php
function get_images($query){
$url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=';
$url .= urlencode($query);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
//decoding request
$result = json_decode($data, true);
return $result;
}
$images = get_images("porsche");
//print_r($images);
foreach ($images['responseData']['results'] as $result) {
echo $result['url']. "<br>";
}
?>
Output
http://upload.wikimedia.org/wikipedia/commons/e/e7/2012_NAIAS_Red_Porsche_991_convertible_(world_premiere).jpg
http://www.inautonews.com/wp-content/uploads/2012/05/porsche-911-club-coupe-1.jpg
http://o.aolcdn.com/hss/storage/adam/6e58b30fb96bf0d28d10fa6d290682e7/porsche-exclusive-911t-cab.jpg
http://media.caranddriver.com/images/media/510773/porsche-960-updated-inline-photo-514518-s-original.jpg
I have client controller that sends request and server controller that processes the request and sends response.
How to paginate the response json data in laravel.
Server Controller
public function index() {
$data = languages::where('is_active','1')->orderBy('id','desc')->get();
$response = Response::json($data,200);
return $response;
}
Client Controller
public function index()
{
$url = url('languagesService');
$data = json_encode(array("username" => $this -> username,"password" => $this -> password));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HEADER,false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = CURL_EXEC($ch);
$data = json_decode($response);
return View::make("languages.index")->with("data",$data);
}
I guess, this is quite simple. Have you tried something like :
public function index() {
$data = languages::where('is_active','1')->orderBy('id','desc')->paginate($perPage);
$response = Response::json($data,200);
return $response;
}
UPD:
In case you want to paginate elsewhere of index() action, then you should create manually the paginator object via native Paginator class. You can find additional info here. As quick example, you should do on your client controller:
$paginator = Paginator::make($data, count($data), $perPage);
And then use the $paginator object to achieve what you want.
You can simply return the collection, it will convert to JSON itself.
public function index() {
return languages::where('is_active','1')->orderBy('id','desc')->paginate();
}
// If you add `page` param into URL, Eloquent gets you content for specified page.
$url = url('languagesService', ['page'=>0]);
// $url = url('languagesService', ['page'=>20]);
I advice you send links for previous/next page together with total count of items so that API consumer can retrieve more elements if needed.
public function index() {
$data [
'data' => languages::where('is_active','1')->orderBy('id','desc')->paginate()->toArray(),
'total' => getNumberOfAllEntitiesThatFitFilter(),
'nextPage' => getUrlForNextPage(),
'previousPage' => getUrlForPreviousPage(),
];
return Response::json($data);
}