PHP to Nodejs conversion for Pipedrive Deals - php

I am getting back into programming after being gone for 20 years. A lot has changed! lol...
I have a NodeJS server setup on Heroku for my mobile app. I am trying to add an event on my server that will add new user info to Pipedrive.com using their API.
They have only written their API examples in PHP. So I'm trying to translate PHP to Javascript, while also learning NodeJS, PHP, and understanding Pipedrive's API all at the same time.
They pointed me to Tonicdev which has been epically useful in getting my javascript syntax down. Since that uses live Pipedrive data when I add my token, I can do all my testing there too, before trying to upload and test on my actual Nodejs server. So that's handy!
But I'm still trying to get a grip on what's happening in their API code. This is my first time to implement an API by myself.
Here is the page I am trying to translate:
http://support.pipedrive.com/hc/en-us/articles/206679239-Creating-a-deal-using-the-REST-API-and-PHP-full-working-example-code-
I don't need the organization. Just the person and the deal.
In my create_person function, I found this php:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $person);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
My questions are:
What is curl_init and curl_setopt?
Do I need them in my Nodejs script file for this?
If not, what javascript do I use in their place?
Thanks for your patience. Learning a ton here!!!

curl is a PHP library that handles making requests.
In this case curl_init() is initialising a new request and curl_setopt is setting certain options.
You'll want to replace curl with an equivalent library for NodeJS. The request module is pretty good for this, although there's plenty of other options too.

Related

AWS Lambda - Invoke by straight PHP curl

Alright,
So I'm aware of the AWS PHP SDK. I do not want to use it to make a curl call to invoke a lambda function.
I've found things about using API Gateway. I also do not want to use API Gateway. I just want to be like:
$endPoint = "https://urltoinvokethefunction"
$ch = curl_init();
$chOptions = [
CURLOPT_URL => $endPoint,
//credentials, headers, etc etc
];
curl_setopt_array($ch, $chOptions);
$result = curl_exec($ch);
Is this at all possible? I want it to be as simple as possible, just a straight curl call to invoke the function. As far as I understand it's what is happening in the SDK anyway, so it must be possible without using the SDK.
I know the most sensible way would be to use the SDK, and I have used it successfully before. I'm up against an unfortunate scenario of trying to add in custom business logic to an outdated, spider web of a CMS, and where the SDK is already included in a plugin I can't simply reuse the already namespaced SDK in a custom function.
tldr; writing a publish_post hook in Wordpress, can't use the AWS SDK because a plugin is already using it, just looking to make a straight curl call to invoke the function, not going to use API Gateway, how to?
****** UPDATE ******
Solved my problem, hanging head low... the reason I couldn't use the already loaded SDK was a versioning issue... d'oh. Stuck using a super old version for the time being, but it works.
The question still remains though, because I'm a stickler for simplicity, and I'd rather just know how to do it myself rather than import a super huge library (sledgehammer to hammer a nail kind of thing). Curious to hear a solution!
$data = array(‘key’=>'key1');
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"ENTER_YOUR_PUBLIC_URL");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, “POST”);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Content-Type: application/json’,
‘Content-Length: ‘ . strlen($data_string))
);
$result = curl_exec($ch);
Using AWS API Gateway is dead simple to link it to your Lambda function. You simply create a resource and stage, associate it to your Lambda function, and then deploy the API. From there, you'll receive a public URL to send requests to that will execute your Lambda function.
To my knowledge, this is the only way to execute a Lambda function over HTTP(S)

how to delete member from chat room xmpp server via php

I am trying to delete a member of chat room from XMPP server via php. I am using curl request for that.
I am following this documentation:
https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-chat-room
$url = "http://188.***.***.***/plugins/restapi/v1/chatrooms/".$roomName."/members/".$userJID;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/xml", "Authorization : ******")); //I am using plugin.userservice.secret key here
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
It should return me http response 201, but I am getting login form of server in response or 401 (unauthorized user).
I am trying to do this since last one week, but did not get any solution of this till, please help me.
Thanks in advance for your kind support.
Please note that this question if very specific: it relates to a specific XMPP server implementation (Openfire) and makes use of a proprietary, non-standard interface (its REST plugin). The fact that you're making use of an Android environment, PHP and/or cURL is irrelevant.
When you receive 401 responses, then there is a problem with authentication.
As Roman points out in a comment below, you're using the wrong documentation. Use this instead!
Two other observations that Roman made (out-of-band):
There's a surplus space character in "Authorization :" It need to be "Authorization:"
The property that you should use is plugin.restapi.secret, not plugin.userservice.secret.
Since this question is already well answered but I will like answer for Android specific context so other user coming to this question can find an alternate way.
There is a library for RestApiClinet for android here. You can integrate it directly as android module. Here is an app already using it. You can also have a look on this client library written in php.

new to curl & PHP: how to use it

I am trying to access some basic info from an XML based API. I'm new to this and am getting lost reading the guides on php.net and via google that seem to assume I already know the basics.
The input needs to be formatted as follows:
<query>
<auth_key>xxxxx</auth_key>
<command>get_account</command>
<account_id>11122</account_id>
</query>
The return will be in an XML format. I assume I need to use CURL to connect and send the input, but I'm lost - how should the PHP code look to do this?
::UPDATE::
okay, I'm still struggling and not making any progress. I've found a tutorial that has kinda lead me to the following code, but it's not doing anything and I can't figure out how/where I'm supposed to acutally send the XML data through to the url.
$URL = 'http://www.test.com';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
print_r($data);
I'm not getting this right and not sure what I'm supposed to be doing. Any help would be appreciated!
The API probably expects you to POST your request to the server, in which case an HTTP POST with cURL example should help. Check the HTTP code of server response (200 is good, others are probably bad) - and if it's good then parse the XML.
Most APIs have very good documentation and examples though. It's worth reading through them or Googling for other people in your shoes.
I ended up stumbling on the following link: http://www.phpmind.com/blog/2009/08/how-to-post-xml-using-curl/
From there I got the sample code working and manipulated it to suit my needs. I also ended up using http://php.net/manual/en/book.simplexml.php for the xml parsing and it seemed very straight forward.

How to process XML returned after submitting a form?

I have just started a project that involves me sending data using POST in HTML forms to a another companies server. This returns XML. I need to process this XML to display certain information on a web page.
I am using PHP and have no idea where to start with how to access the XML. Once I knwo how to get at it I know how to access it using XPath.
Any tips of how to get started or links to sites with information on this would be very useful.
You should check out the DOMDocument() class, it comes as part of the standard PHP installation on most systems.
http://us3.php.net/manual/en/class.domdocument.php
ohhh, I see. You should set up a PHP script that the user form posts to. If you want to process the XML response you should then pass those fields on to the remote server using cURL.
http://us.php.net/manual/en/book.curl.php
A simple example would be something like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://the_remote_server");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
$YourXMLResponse = curl_exec($ch);
?>

Integration of escrow web services in a PHP site

I want to integrate the services of escrow.com in my PHP site.
How would you get started with this goal, and what APIs provided would be the basic functionality? Do you have any PHP specific advice or gotchas? Would you recommend another service provider?
I'm working on an API project with this Company at the moment. I know looking at the documentation it all looks a little daunting, however, you can get away with making it as simple as a small cURL request.
I'd suggest starting with the "New escrow transaction" example provided, and build your request using the provided XML they offer, amended with your details.
Assign the XML to a variable, and pass it through a curl request similar to the below;
// Initialise your cUrl object
$ch = curl_init('https://xml.Escrow.com/Invoke/Partners/ProcessrequestXML.asp');
//set your cURL options
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "\$xmldata=".urlencode($xml));
//Start your cURL Transaction
ob_start();
//execute your cURL object with your parameters
$result = curl_exec($ch);
//set the returned info to a variable
$info = curl_getinfo($ch);
// close the transaction
curl_close ($ch);
//get the contents of the transaction
$data = ob_get_contents();
ob_end_clean();
//optional; Redirect to a specific place
header("Location:".$url);
The only advise I can offer is to read through the documentation carefully, and always check the values you are passing in.
Where possible, it is also a good idea to segregate the API functions into their own class, this will make maintenance and troubleshooting, as well as testing the functionality that much easier.
This is the first time I hear about escrow, but a quick scan of the site gives me:
this contact form to get more info:
https://escrow.com/contact/sales.asp
A FAQ:
https://www.escrow.com/support/faq/index.asp?sid=8
www.Transpact.com offers a similar but lower cost service.
It is also UK Government (FSA and HMRC) registered.
It offers a simple SOAP API for easy integration into your website.

Categories