Magento calls not happening when calling from inside a function - php

I am trying to create a user in Magneto using XMLRPC inside PHP. The call is successful when calling it directly from a page, however, if I place the same call inside a function, it is not happening at all. Any ideas?
Code directly on page: [ Working perfect]
<?php
require 'Zend/XmlRpc/Client.php';
$client = new Zend_XmlRpc_Client('myclient.com/api/xmlrpc/');
$session = $client->call('login', array('myuser', 'mypass'));
$new_customer = array('email' => 'nasr#di91.com','firstname' => 'Nair','lastname' => 'Perwaiz','password' => '123456','website_id' => 1,'store_id' => 1,'group_id' => 1);
$zendSpecificArray =array(Zend_XmlRpc_Value::getXmlRpcValue($new_customer,Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT));
$rArray = array($session,'customer.create',$zendSpecificArray);
$new_customer_id = $client->call('call',$rArray);
$rtnval='<?xml version="1.0"?>'.PHP_EOL;
$rtnval.='<root>'.PHP_EOL;
$rtnval.='<result>'.'Customer created with ID :'.$new_customer_id .'</result>'.PHP_EOL;
$rtnval.='</root>'.PHP_EOL;
$client->endSession($session);
header('Content-Type:','Application/xml');
echo $rtnval;
?>
Code inside the function: [No output being received]
function registerUser($email,$firstname,$lastname,$password)
{
$client = new Zend_XmlRpc_Client('myclient.com/api/xmlrpc/');
$session = $client->call('login', array('myuser', 'mypass'));
$new_customer = array('email' => $email,'firstname' => $firstname,'lastname' => $lastname, 'password' => $password,'website_id' => 1,'store_id' => 1,'group_id' => 1);
$zendSpecificArray =array(Zend_XmlRpc_Value::getXmlRpcValue($new_customer,Zend_XmlRpc_Value::XMLRPC_TYPE_STRUCT));
$rArray = array($session,'customer.create',$zendSpecificArray);
$new_customer_id = $client->call('call',$rArray);
$rtnval='<?xml version="1.0"?>'.PHP_EOL;
$rtnval.='<root>'.PHP_EOL;
$rtnval.='<result>'.'Customer created with ID :'.$new_customer_id .'</result>'.PHP_EOL;
$rtnval.='</root>'.PHP_EOL;
$client->endSession($session);
header('Content-Type:','Application/xml');
echo $rtnval;
}
Call : http://localhost/xxx/rpcclient/rpc.php?methodname=registeruser&em=uuuy#gmail.com&f=ab&l=ty&p=kaddoo

Ran a wireshark trace and found that my program was not catching the fault. SO fixed it. Thanks a lot everyone!

Related

GoCardless API - List Subscriptions

I am using the GoCardless Documentation here to try list all subscriptions for a customer.
I have followed the instructions as you can see below, however nothing at all is displaying when I run this script - does anyone know what I may have done wrong?
require 'vendor/autoload.php';
$client = new \GoCardlessPro\Client(array(
'access_token' => 'XXXXXx',
'environment' => \GoCardlessPro\Environment::LIVE
));
$client->subscriptions()->list([
"params" => ["customer" => "CU000R3B8512345"]
]);
Calling a method on its own doesn’t do anything. It’ll execute the given method, but it’s not going to print anything to your browser screen on its own.
As RiggsFolly says (and is documented in GoCardless’s API documentation), calling $client->subscriptions()->list() will return a cursor-paginated response object. So you need to do something with this result. What that is, I don’t know as it’s your application’s business logic and only you know that.
<?php
use GoCardlessPro\Client;
use GoCardlessPro\Environment;
require '../vendor/autoload.php';
$client = new Client(array(
'access_token' => 'your-access-token-here',
'environment' => Environment::SANDBOX,
));
// Assign results to a $results variable
$results = $client->subscriptions()->list([
'params' => ['customer' => 'CU000R3B8512345'],
]);
foreach ($results->records as $record) {
// $record is a variable holding an individual subscription record
}
Pagination with Gocardless:
function AllCustomers($client)
{
$list = $client->customers()->list(['params'=>['limit'=>100]]);
$after = $list->after;
// DO THINGS
print_r($customers);
while ($after!="")
{
$customers = $list->records;
// DO THINGS
print_r($customers);
// NEXT
$list = $client->customers()->list(['params'=>['after'=>$after,'limit'=>100]]);
$after = $list->after;
}
}

Calling a function inside a foreach goes to a different place than outside the foreach?

Wow talk about a mind bender. Ok, so whenever I run this
<?php
require_once "../vendor/autoload.php";
use Minishlink\WebPush\WebPush;
use Minishlink\WebPush\Subscription;
$public_key = "PUBLIC_KEY_REDACTED";
$private_key = "PRIVATE_KEY_REDACTED";
$auth = array(
'VAPID' => array(
'subject' => 'https://github.com/Minishlink/web-push-php-example/',
'publicKey' => $public_key, // don't forget that your public key also lives in app.js
'privateKey' => $private_key, // in the real world, this would be in a secret file
)
);
$json = json_decode(file_get_contents("php://input"));
file_put_contents("notification_subscription_info.txt", file_get_contents("php://input"));
try {
$subscription = Subscription::create(json_decode(file_get_contents('php://input'), true));
$notification = [
'subscription' => Subscription::create([ // this is the structure for the working draft from october 2018 (https://www.w3.org/TR/2018/WD-push-api-20181026/)
"endpoint" => "{$json->endpoint}",
"keys" => [
'p256dh' => "{$json->keys->p256dh}",
'auth' => "{$json->keys->auth}"
],
]),
'payload' => 'Hello!',
];
$webPush = new Minishlink\WebPush\WebPush($auth);
$webPush->sendNotification(
$subscription,
$notification['payload'] // optional (defaults null)
);
//version 1 (outside the foreach)
$webPush->flush();
//version 2 (inside the foreach)
foreach($webPush->flush() as $report) { } //This can be empty
} catch (Exception $e) {
echo $e->getMessage();
}
I get very weird behavior in my debugger phpstorm. If I run it with version 1 then I get no push notification and it calls a __destruct method.
If I run version 2 however I get normal behavior and the push notification sends successfully. It appears as if by merely being inside the foreach it changes the flow of the program. I have no idea why this would be and I'm left scratching my head. Does anyone know what is happening?
Edit: I was mistaken about the __destruct method. That's being called because it's the end of the function. In the debugger it's skipping over the flush() call as though it's a literal value. So if I do $report = $webPush->flush(); it will set $report without ever even calling the flush() function (at least that's what it seems like). I even have a breakpoint at the beginning of the flush() function and it isn't hitting it.

adding script tag in shopify using php

We are using this GitHub PHP library for adding javascript in to the head of shopify pages using script tag but we have got stuck somewhere,
it redirects well. We go to app screen to get permission also when we click on install it redirects to redirect page and gives error. in my error log.
Uncaught PHPShopify\Exception\ApiException: script_tag -
expected Array to be a Hash in
This is our app code
<?php
require '/home/xxx/public_html/shopify/1/vendor/autoload.php';
$config = array(
'ShopUrl' => 'xyyy.myshopify.com',
'ApiKey' => 'a07235d5cxx4af2239ea02fe197',
'SharedSecret' => '7ae8a450xxxx2576cf5e7a606c3',
);
PHPShopify\ShopifySDK::config($config);
$shopify = new PHPShopify\ShopifySDK;
$scopes = array('read_orders','read_script_tags','read_products', 'write_script_tags');
$redirectUrl = 'https://xxxx.com/shopify/1/99.php/auth/callback';
$auth = \PHPShopify\AuthHelper::createAuthRequest($scopes, $redirectUrl);
$src = "https://xxxx.com/modules/script72paid.js";
$finalurl='https://xxxxx.myshopify.com/admin/script_tags.json'.
$shopify->ScriptTag->post(array("post"), ''.$finalurl.'', array( "script_tag" => array( "event"=>"onload", "src"=>$src)));
?>
and this is our redirect link code
<?php
require '/home/xxxxxx/public_html/shopify/1/vendor/autoload.php';
$config = array(
'ShopUrl' => 'xxxx.myshopify.com',
'ApiKey' => 'a07235d5cxxxxxx9ea02fe197',
'SharedSecret' => '7ae8a45xxxxxxx76cf5e7a606c3',
);
PHPShopify\ShopifySDK::config($config);
$shopify = new PHPShopify\ShopifySDK;
$accessToken = \PHPShopify\AuthHelper::getAccessToken();
$config2 = array(
'ShopUrl' => 'xxxx.myshopify.com',
'AccessToken' => $accessToken,
);
$shopify2 = new PHPShopify\ShopifySDK($config2);
$src = "https://xxxxx.com/modules/script72paid.js";
$finalurl='https://xxxxx.myshopify.com/admin/script_tags.json'.
$shopify2->ScriptTag->post(array("post"), ''.$finalurl.'', array( "script_tag" => array( "event"=>"onload", "src"=>$src)));
?>
How do we avoid this error with expected Array to be a Hash?
Any help will be great.
Check that you are passing the correct parameters to $shopify->ScriptTag->post() by looking at the function in your copy of the library. Different versions of the library and documentation may have changed something.
Check that those parameters are in the correct order.
Check that all of the arrays you are passing are not actually meant to be objects.
Check if any of the arrays need to wrapped in another array.

Sightengine API not working correctly?

I have recently come across an API called Sightengine which checks images for nudity, violence and some more things. I have followed their site to set up this API and got the following code in my PHP-file:
require __DIR__."/../Sightengine/src/SightengineClient.php";
require __DIR__."/../Sightengine/src/Check.php";
$filterImage = new Sightengine\SightengineClient('189981095', '5snnKqMiPU8cx2fHuUff');
$resultFilterImage = $filterImage->check(['nudity'])->set_url('https://d3m9459r9kwism.cloudfront.net/img/examples/example7.jpg');
Now, this should work and give back a JSON with information in it about how nude the picture is, but when trying to run this, it gives me the follow error:
Fatal error: Uncaught Error: Class 'GuzzeleHttp\Client' not found in C:\xampp\htdocs\ugesco\Sightengine\src\SightengineClient.php:13
Stack trace:
#0 C:\xampp\htdcos\ugesco\Controller\controller.php(234):
Sightengine\SightengineClient->__construct('my user key', 'my pass key')
#1 {main}
thrown in C:\xampp\htdocs\Ugesco\Sightengine\src\SightengineClient.php on line 13
Now, what I get out of this is that the class GuzzleHttp\Client can't be found in the SightengineClient.php-file. Thing is, I copied this from the Sightengine-company their Github, so I have no clue why it is not there..?
This is the code in the SightengineClient.php:
<?php
namespace Sightengine;
class SightengineClient {
private $api_user;
private $api_secret;
private $endpoint = 'https://api.sightengine.com/';
private $http;
function __construct($api_user, $api_secret) {
$this->api_user = $api_user;
$this->api_secret = $api_secret;
$this->http = new \GuzzleHttp\Client(['base_uri' => $this->endpoint, 'User-Agent' => 'SE-SDK-PHP' . '1.0']);
}
public function feedback($model, $modelClass, $image) {
$url = '1.0/feedback.json';
if (filter_var($image, FILTER_VALIDATE_URL)) {
$r = $this->http->request('GET', $url, ['query' => ['api_user' => $this->api_user, 'api_secret' => $this->api_secret, 'model' => $model,'class' => $modelClass,'url' => $image]]);
return json_decode($r->getBody());
}
else {
$file = fopen($image, 'r');
$r = $this->http->request('POST', $url, ['query' => ['api_user' => $this->api_user, 'api_secret' => $this->api_secret, 'model' => $model,'class' => $modelClass],'multipart' => [['name' => 'media','contents' => $file]]]);
return json_decode($r->getBody());
}
}
public function check($models) {
return new Check($this->api_user, $this->api_secret, $models);
}
}
As you can see on line 13 it wants to make a new GuzzeleHttp\Client but that Client-class is nowhere to be found. You can check their Github yourself: https://github.com/Sightengine/client-php
This is a real company who offers packages of image checks for money, so how could this be that their code is not working anymore (or is this an easy fix?).
Thanks in advance!
It does work if you perform a proper install using composer:
composer require sightengine/client-php
guzzlehttp/guzzle is correctly listed as a dependency in the composer.json file: https://github.com/Sightengine/client-php/blob/master/composer.json

Webservice SOAP request - Wrong type of data sent

Context / What I want :
I'm facing an issue while calling a Webservice with SOAP. Here's an image the relevant part of the WS I want to call :
(I voluntarily hide the namespace part, not relevant here)
I want to send data through 'Demande_de_mot_de_passe' function and catch result from this request.
In the code below, this request is correct (the name of the function is good), I guess the problem is the formatting of the data I want to send. The call of the function is made with this part :
$client->Demande_de_mot_de_passe($soapVar);
What I've tried :
Here's the relevant part of the code I've tried ( I voluntarily change values of data but nothing else. There is no typo error with the brackets, it close the function and the class I didn't put here to keep the relevant part) :
$client = new \SoapClient('URL_OF_THE_WS?WSDL', array(
'trace' => 1,
'encoding' => 'UTF-8',
'soap_version' => SOAP_1_1,
'classmap' => array('Demande_de_mot_de_passe_Input' => 'Demande_de_mot_de_passe_Input')
));
$donnesUtilisateur = new Demande_de_mot_de_passe_Input;
$donnesUtilisateur->Code_societe = '000';
$donnesUtilisateur->Ident_type = 'A';
$donnesUtilisateur->Ident_code = 'xxxxxx';
$donnesUtilisateur->Dat_demande = '00000000';
$donnesUtilisateur->Adr_mail = 'xxxxxx';
$donnesUtilisateur->Adr_cpos = 'xxxxxx';
$donnesUtilisateur->Nom = 'xxxxxx';
$donnesUtilisateur->Prenom = 'xxxxxx';
$donnesUtilisateur->Dat_naiss = '00000000';
$namespace = 'URL_OF_NAMESPACE';
$soapVar = new \SoapVar($donnesUtilisateur, SOAP_ENC_OBJECT,'Demande_de_mot_de_passe_Input', $namespace);
$result = $client->Demande_de_mot_de_passe($soapVar);
print_r($result);
}
}
class Demande_de_mot_de_passe_Input {
public $Code_societe;
public $Ident_type;
public $Ident_code;
public $Dat_demande;
public $Adr_cpos;
public $Adr_mail;
public $Nom;
public $Prenom;
public $Dat_naiss;
}
I've also tried with passing array of casting an object with the array like this (without success) :
$donnesUtilisateur = [
'Code_societe' => '000',
'Ident_type' => 'A',
'Ident_code' => 'xxxxxx',
'Dat_demande' => '00000000',
'Adr_cpos' => 'xxxxxx',
'Adr_mail' => 'xxxxxx',
'Nom' => 'xxxxxx',
'Prenom' => 'xxxxxx',
'Dat_naiss' => '00000000',
];
and :
$donnesUtilisateur = (object) [
'Code_societe' => '000',
'Ident_type' => 'A',
'Ident_code' => 'xxxxxx',
'Dat_demande' => '00000000',
'Adr_cpos' => 'xxxxxx',
'Adr_mail' => 'xxxxxx',
'Nom' => 'xxxxxx',
'Prenom' => 'xxxxxx',
'Dat_naiss' => '00000000',
];
Error I get :
SoapFault: Did not receive a 'Demande_de_mot_de_passe_Input' object. in SoapClient->__call()
If I unterstand clearly, the formatting of data sent is not correct but when I try other way to send it, it still reporting the same error.
Docs I've read about without success :
http://www.fvue.nl/wiki/Php:_Soap:_How_to_add_attribute_to_SoapVar
http://grokbase.com/t/php/php-soap/066jkmcz2h/passing-objects-to-soap-server-complextype-classmap
EDIT
Here's a capture of the WS 'Demande_de_mot_de_passe' function call in SoapUI :
(Sorry for the long post, I hope it is clear enough, don't forget to ask about precisions if needed, thanks in advance for your help :) )
At your WSDL's type, there's a sequence named Demande_de_mot_de_passe which use a element named Demande_de_mot_de_passeRequest and not Demande_de_mot_de_passe_Input.
Your print from SoapUI describe the message request, but if it's document style, Demande_de_mot_de_passe is a type. On the other hand if it's RPC is the method name.
Starting if it's RPC you can do as showed below. You should use as native object as you can (SOAP will work better with they). A stdObject will be good enough:
$request = new stdClass();
$demande_de_mot_de_passeRequest->Code_societe = '000';
$demande_de_mot_de_passeRequest->Ident_type = 'A';
$demande_de_mot_de_passeRequest->Ident_code = 'xxxxxx';
$demande_de_mot_de_passeRequest->Dat_demande = '00000000';
$demande_de_mot_de_passeRequest->Adr_mail = 'xxxxxx';
$demande_de_mot_de_passeRequest->Adr_cpos = 'xxxxxx';
$demande_de_mot_de_passeRequest->Nom = 'xxxxxx';
$demande_de_mot_de_passeRequest->Prenom = 'xxxxxx';
$demande_de_mot_de_passeRequest->Dat_naiss = '00000000';
$request->Demande_de_mot_de_passeRequest = $demande_de_mot_de_passeRequest;
$response = $client->Demande_de_mot_de_passe($request);
If your SOAP binding is document, you just have to add a new upper level named Demande_de_mot_de_passe
/** The variable $demande_de_mot_de_passeRequest is created as above **/
$demande_de_mot_de_passe = new stdClass();
$demande_de_mot_de_passe->Demande_de_mot_de_passeRequest = $demande_de_mot_de_passeRequest;
$request->Demande_de_mot_de_passe = $demande_de_mot_de_passe;
$response = $client->Demande_de_mot_de_passe($request);
Your WSDL doesn't need a list/collections (it's not your case), so you don't need to create/parse variables with SoapVar. There's others examples that you can read about (one is mine, but it's in portuguese) and other is about the BOGUS node:
http://forum.imasters.com.br/topic/535213-enviar-xml-via-soap/?p=2137411
http://www.fischco.org/blog/2011/3/26/php-soapserver-objects-arrays-and-encoding.html

Categories