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);
Related
why some url with json content return null on php function get page content?
i used these ways :
file_get_content
curl
http_get
but return null . but when page open with browser json content show on browser ?? anyone can help me?
$url = 'https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/?access_token=9IaRpdmQzFppJMabLHbHyzBaQnm8bM';
$result = file_get_content($url);
return null but in browser show
{"error_description": "Access token has expired.", "error": "invalid_credentials"}
It returns the following:
failed to open stream: HTTP request failed! HTTP/1.1 401 UNAUTHORIZED
So, you have to be authorized.
You ca play with this code snippet (I can't test since token expired again):
<?php
$access_token = "s9spZax2Xrj5g5bFZMJZShbMrEVjIo"; // use your actual token
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/validate/com.pdgroup.insta/inapp/coin_follow_1/purchases/324234235/";
// Solution with file_get_contents
// $content = file_get_contents($url . "?access_token=" . $access_token);
// echo $content;
// Solution with CURL
$headers = array(
"Accept: application/json",
"Content-Type: application/json",
"Authorization: Basic " . $access_token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url); // . $params - if you need some params
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_USERPWD, $userPwd);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($ch_error) {
throw new Exception("cURL Error: " . $ch_error);
}
if ($info["http_code"] !== 200) {
throw new Exception("HTTP/1.1 " . $info["http_code"]);
}
echo $result;
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);
}
When I try to send request to the server in localhost I receive this message :
Error: call to URL
https://wasl.elm.sa/WaslPortalWeb/rest/DriverRegistration/send failed
with status 200, response {"resultCode":503,"resultMessage":"Yakeen
Failure: The ID Is Not Found at
NIC","referenceNumber":0,"valid":false}, curl_error , curl_errno 0
On the live server I receive this message :
string(171) "{"apiKey":"My api
key","captainIdentityNumber":"12355567897","dateOfBirth":"15-07-1400","emailAddress":"mo#mo.com","mobileNumber":"966508060095"}"
Error: call to URL
https://wasl.elm.sa/WaslPortalWeb/rest/DriverRegistration/send failed
with status 0, response , curl_error Unknown SSL protocol error in
connection to wasl.elm.sa:443 , curl_errno 35
I use this script to make the request:
<?php
//driver registartion url
$url = "https://wasl.elm.sa/WaslPortalWeb/rest/DriverRegistration/send";
$data=["apiKey"=>"My apikey","captainIdentityNumber"=>"1234567897","dateOfBirth"=>"15-07-1400","emailAddress"=>"mo#mo.com","mobileNumber"=>"966508060095"];
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER,false);
curl_setopt($curl, CURLOPT_HTTPHEADER,array("Content-type: application/json"));
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
var_dump($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);
var_dump($response);
?>
my api key it's correct one given by the server owner
This is related to Wasl portal, it is not related to curl or connection errors.
You have to contact the portal admins because it is API specific error.
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);
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);
}