Issue with PHP curl / json POST - php

I am running into an issue trying to issue a POST to the chargify API log payments from within a form.
However, it appears that the POST is actually being sent as a GET for some reason as it's returning an error.
Can anyone offer any advice on what I may be doing wrong? Postman is working just fine sending a POST with the JSON as the body, but I can't get it to work from PHP.
(note: the <urlmaskedforprivacy> is obviously not in my actual code)
//find invoice id # using invoice number search form
$invoiceNumber = fRequest::get('invoicenumber');
$json = file_get_contents('https://<maskedforprivacy>.chargify.com/invoices.json?number=' . $invoiceNumber);
$returnedInvData = fJSON::decode($json);
$invoiceId = $returnedInvData[0]->invoice->id;
// grab form data as payment info to send to chargify
$paymentAmount = fRequest::Get('amount');
$memo = fRequest::Get('memo');
if ((isset($invoiceNumber,$paymentAmount,$memo))) {
$url = 'https://<maskedforprivacy>.chargify.com/subscriptions/' . $invoiceId . '/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = FJSON::encode($params);
// send to chargify
$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/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

I've used this one code, but it returns 401 error and asks for Basic auth. in json_response variable. May be you've missed it?
$paymentAmount = 10;
$memo = 'test';
$url = 'https://test.chargify.com/subscriptions/1/payments.json';
$params = array(
'payment' => array(
'amount' => $paymentAmount,
'memo' => (string)$memo
),
);
// encode as json
$content = json_encode($params);
// send to chargify
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);

Related

How can I deregister a device if I only have that device's push token, not the HWID?

My database is only storing the push tokens for user devices, not the HWIDs.
Is it possible to use the Pushwoosh API to deregister a device with the push token only? It's not working when I send just the push token with an empty string for the hwid.
Here's my code:
$device_id = '';
$pushwoosh_push_token = 'Push-Token-here';
$pushwoosh_app_code = 'App-Code-here';
$pushwoosh_auth_token = 'Auth-Token-here';
$response = unregister_device_pushwoosh($device_id, $pushwoosh_push_token, $pushwoosh_app_code, $pushwoosh_auth_token);
print_r($response)."<br>";
function unregister_device_pushwoosh($device_id, $pushwoosh_push_token, $pushwoosh_app_code, $pushwoosh_auth_token) {
$url = 'https://cp.pushwoosh.com/json/1.3/unregisterDevice';
$data = array(
'request' => array(
'application' => $pushwoosh_app_code,
'push_token' => $pushwoosh_push_token,
'hwid' => $device_id,
),
);
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Content-type: application/json\r\n".
"Authorization: Basic ".base64_encode("$pushwoosh_app_code:$pushwoosh_auth_token")
]);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (200 != $status)
{
echo("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ', curl_errno ' . curl_errno($curl) . '<br>');
}
curl_close($curl);
return 'Response: ' . $json_response . "\n";
}

UPDATE records using REST api

I am working on PHP and Salesforce integration and I want to update records using PHP.
I want to update records in Salesforce when clicking on the Button "Follow up".
And also try the below code.
function update_account($id, $followup, $instance_url, $access_token) {
$url = "$instance_url/services/data/v20.0/sobjects/Contact/$id";
$content = json_encode(array("shivendra__FollowUp__c" => $followup));
echo $content;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token",
"Content-type: application/json"));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 204 ) {
die("Error: call to URL $url failed with status $status, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
echo "<b>update_contact function</b>";
echo "HTTP status $status updating Contact<br/><br/>";
curl_close($curl);
}
But getting an error:
{"shivendra__FollowUp__c":null}
[{"errorCode":"METHOD_NOT_ALLOWED","message":"HTTP Method 'PATCH' not allowed. Allowed are HEAD,GET,POST"}]
Error: call to URL https://shivendra-dev-ed.my.salesforce.com/services/data/v20.0/sobjects/Contact/ failed with status 405, curl_error , curl_errno 0
Try the post method with cURL, similar to this:
curl_setopt($ch, CURLOPT_POST, 1);

Second request to API results in empty string

First request to API:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Account');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
$response = json_decode($response, true);
I get accounts info as expected. But when i try to do next request after some manipulations with accounts, in response i get empty string.
There is an example of next request:
$siteUrl = $this->getConfig()->get('siteUrl');
$headers = array(
"Espo-Authorization: " . $this->getEncodedLoginData()
);
$curl = curl_init($siteUrl . '/api/v1/Contacts');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
if (!$response) {
$GLOBALS['log']->addWarning('curlerr: ' . curl_error($curl));
}
curl_close($curl);
Doing requests to API from browser works properly.
Problem were in login/pass data. I don't know exactly how, but login/pass vars were changed before executing second request. Thanks for assistance!

sending a response to another php file using $curl returns an error

I am trying to send JSON data to another php file for now for learning purpose (actually meant to be sent to a react-native app) but this returns an error saying
Error: call to URL http://localhost/hhh.php failed with status 200, response , curl_error , curl_errno 0
if($cliSeq==0)
{
$welcomearr= array("welcome");
$url = "http://localhost/hhh.php";
$welcomeJSON = json_encode($welcomearr);
$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/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $welcomeJSON);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

How can I forward PayPal IPN to another PHP file?

We are updating our system and the old IPNs need to be processed differently. However, I cannot change the original IPN send-to addresses. So I have to "bounce" the IPN message off of old_IPNProcessor.php and forward it to new_IPNProcessor.php. I cannot modify old_IPNProcessor.php for technical reasons that go beyond the scope of this question.
From what I have read, I have to use cURL to accomplish this. I have seen some code for this, but I am unsure as to how it's implemented. For instance, the $post var in the code below (from https://stackoverflow.com/a/6213693/1390395) is the raw json straight from PayPal. Does it have to be in a PHP array for this to work?
function forwardIPN($post){
$url = "new_IPNProcessor.php";
// $post is the ipn message from PayPal
$content = $post;
$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/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 201 ) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
}

Categories