Codeigniter restclient not working - php

I am trying to install the CodeIgniter RESTClient and RESTServer libraries for my solution. (philsturgeon-codeigniter-restclient and chriskacerguis-codeigniter-restserver ).
I managed to get the rest server up and running, but I am encountering issues with the rest client.
These are the steps I did to come where I am now:
Copy the Rest.php file (downloaded from GitHub) and put it in the libraries folder
Download the Curl library and put it in libraries
Modified the code in Rest.php to uncomment $this->_ci->load->library('curl'); (if I hover over the usages of the curl library in this file I get the following message):
Field 'curl' not found in CI_Controller
I create a new controller called "Restclient" to test my API. In this controller I created the following method:
function rest_client_example($id)
{
$this->load->library('rest', array(
'server' => 'localhost/codeigniter/api/users/'
));
$user = $this->rest->get('volunteer', array('id' => $id), 'json');
var_dump($user);
}
Browsing to http://localhost/codeigniter/api/restclient/rest_client_example/25 then gives me
D:\wamp\www\codeigniter\application\controllers\api\Restclient.php:36:null
When executing the following code instead of the above, I do get a correct result:
$this->load->library('curl');
$t = $this->curl->simple_get('api/users/volunteer', array('id'=>$id));
var_dump($t);
So I do know that the curl is working.
My guess is that I am doing something wrong with the loading of the curl library?

I know your question is specific to the Libraries mentioned here. Have you tried anything else? I've had really good success with guzzle http
https://github.com/guzzle/guzzle

Related

A way to call a command prompt function from controller in Laravel?

I'm trying to make a thumbnail in a Laravel project and I tried a lot other ways but with no success - Libraries, APIs... The problem is that the project is developed in Windows and the professor needs it in Windows as well.
So far, I experimented to integrate different libraries (wkhtmltoimage, spatie/browsershot, mpdf and s.o.) but in the most of the cases, there are problems with the path.
The required function, which I need, works very good in command prompt and I thought that I must find a way to call it in the controller.
I've tried with:
shell_execute($call);
system($call);
exec($call);
// with $call = "{func_name} {path_to_input_file} {name_of_output_file}";
// Example: $call = "wkhtmltoimage C:\xampp\htdocs\app\public\test.html img.jpg"
But no result. The function generates an image, which I want to store in the database.
Is there an other way to make a call to the command prompt?
Maybe SSH call?
You can execute Artisan commands directly via your controller.
Look at this example from the Laravel documentation:
Route::get('/foo', function () {
$exitCode = Artisan::call('email:send', [
'user' => 1, '--queue' => 'default'
]);
//
});

How laravel classes are included?

I want to run custom php code in laravel directly without using any routes or http requests..
I hope I can make it clear, I mean, like those online tools that runs php code by writing php code in browser, and then run it, and view result..
I found this handy project (Run-PHP-Code) to run PHP in browser directly, but I can't use models of my laravel project in PHP code..
How can I include laravel 's environment, so that I can for example:
$tag= new Tag;
where Tag is a model in laravel project, that would result into:
Fatal error: Class 'Tag' not found in D:\xampp\htdocs\widgetsRepository\app\controllers\Run-PHP-Code-master\index.php(49) : eval()'d code on line 3
Any idea? this would be very useful!
EDIT
I tried Brian suggestion at his answer, but I got this error now:
Call to a member function connection() on null
at vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php
public static function resolveConnection($connection = null)
{
return static::$resolver->connection($connection);
}
so, I think I only need to get database sorted, then I can do experiments easily..
I've never tried to run code from a laravel project directly, I just copy and paste parts of the code into Run PHP Code.
That being said, it should be possible to run the code using the method taken from this StackOverflow question:
The Composer autoload script, to autoload all of your classes:
require __DIR__.'/../bootstrap/autoload.php';
And if you need things from the IoC container, you'll:
$app = require_once __DIR__.'/../bootstrap/start.php';
Then you will be able to do things like:
$post = Post::find(1);

Using angelleye paypal with simplemvc framework

I have used composer to download the Angell EYE PayPal library into my vendor directory. Now I'm trying to call the class within a controller.
I've tried various methods:
Use \angelleye\PayPal;
at the top of page. I've tried using the require() method.
Within the controller I have used
$paypal = PayPal::PayPal($payment);
And a few other ways, but I just get the error Class not found at line 179 and I'm not sure why.
You just need to load a config file (depending on your framework) and the autoloader.
require_once('includes/config.php');
require_once('vendor/angelleye/paypal-php-library/autoload.php');
Of course, adjust the paths to suit where you have those saved, but the autoloader is what makes the classes available to you.
If you want more direct help you can submit a ticket here.
Thanks for response.
I actually managed to get it working on the framework.
I did nt have to load anything or require the class as the composer autoload must do it for me in the framework.
I simply added :
$PayPal = new \angelleye\PayPal\PayPal($PayPalConfig);
and it started to work.
Im guessing if i want to use the PayFlow i would call using:
$PayPal = new \angelleye\PayPal\PayFlow($PayPalConfig);
I will definately post back if the rest of the proccess fails to work.

Unirest POST request won't run in PHP

I have a block of code that's almost as barebones as it gets, but for some reason, it just refuses to run:
<?php
require_once "unirest/src/Unirest.php";
$photo_url = "http://api.animetrics.com/img/test/sc.jpg";
// These code snippets use an open-source library.
$response = Unirest::post("<--URL-->",
array(
"X-Mashape-Key" => "<--API Key-->",
"Content-Type" => "application/x-www-form-urlencoded",
"Accept" => "application/json"
),
array(
"selector" => "FACE, EYES, FULL",
"url" => "http://api.animetrics.com/img/test/sc.jpg"
)
);
echo $response;
?>
This code block was taken directly from the mashape website, and I simply downloaded the Unirest files. I'm also sure that my path is correct.
I investigated a bit and tried adding a static class function into the Unirest file to print something out, and unsurprisingly, it didn't work.
Here's the code chunk I added:
<?php
namespace Unirest;
echo "in file";
$file = new File();
$file->printa("abc");
class File
{
public static function printa($a) {
echo $a;
}
....
Within the same file, $file->printa("abc"); worked perfectly, but when called from a different file, File::printa("abc"); or Unirest::printa("abc"); or File\Unirest::printa("abc"); just refuse to run.
I'm not sure but am I misunderstanding something about namespaces? I would have thought that Unirest::printa("abc"); is the correct way to access a static class function?
I'd appreciate any advice regarding this, thanks.
Since the release of Unirest 2.0, the method & class signature has changed. unfortunately the Mashape sample snippets are yet to be updated.
Instead of calling Unirest::post you should be calling Unirest\Request::post, please refer to the unirest documentation for more details.
We'll be updating the Mashape samples soon to reflect this change.
I'm the author of unirest-php and I work at Mashape.

alchemy api rest api endpoint is giving error 404 not found

i am trying to integrate alchemy api in my project which is in php.
before few days everything was working fine but now the rest api endpoint http://access.alchemyapi.com/ which i called in my program is giving error 404 not found
please if any can tell why it is giving this error.
following is the snippet of code where i have called the rest api
<?php
class AlchemyAPI {
private $_api_key;
private $_ENDPOINTS;
private $_BASE_URL = 'http://access.alchemyapi.com/calls';
*/
public function AlchemyAPI() {
//Load the API Key from api_key.txt
$key = trim(file_get_contents("api_key.txt"));
$this->_api_key = $key;
//Initialize the API Endpoints
$this->_ENDPOINTS['sentiment']['url'] = '/url/URLGetTextSentiment';
$this->_ENDPOINTS['sentiment']['text'] = ' /text/TextGetTextSentiment';
$this->_ENDPOINTS['sentiment']['html'] = '/html/HTMLGetTextSentiment';
/* rest of the code */
?>
I just cloned the PHP SDK from the AlchemyAPI GitHub repo and was able to run the example just fine. Try running the example with: php example.php
You can also try running the tests with: php tests.php. It calls each API endpoint and that should tell you if one of them is causing an issue.
If that doesn't work, if you have modified the alchemyapi.php file, you can try undoing your changes or perhaps re-cloning the repo. I'm not sure if it's just a copy/paste issue, but it looks like there might be a wayward */ in your code snippet.

Categories