+ Start a Discussion
Ismael AcevedoIsmael Acevedo 

Error Rest API

Can you help me ,the error that i get is Error: call to URL https://login.salesforce.com/services/oauth2/token failed with status 0
 
<?php

$id=$_POST["id"];
define("CLIENT_ID", "");
define("CLIENT_SECRET", '');
define("LOGIN_URI", "https://login.salesforce.com");

//define("SF_SECURITY_TOKEN", "");
define("USERNAME", "");
define("PASSWORD", "");

function salesforce_login($username, $password) {
    $params = "grant_type=password&client_id=".CLIENT_ID.
            "&client_secret=".CLIENT_SECRET.
            "&username=$username&password=$password";

    $token_url = LOGIN_URI . "/services/oauth2/token";

    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 200 ) {
        die("Error: call to URL $token_url failed with status $status");
    }

    return json_decode($json_response, true);
}

$response = salesforce_login(USERNAME, PASSWORD);
$access_token = $response["access_token"];
$instance_url = $response["instance_url"];

$user = $id;
$url = "$instance_url/services/data/v20.0/sobjects/User/$user";

$content = json_encode(array("IsActive" => false));

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
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);

$response = curl_exec($curl);

$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ( $status != 204) {
    die("Error: call to URL $url failed with status $status, response $response\n");
}

echo "Successfully updated user $user\n";

// -------------------Send message to the ajax--------------------------
$response = array('status' => $status);
echo json_encode($response);

curl_close($curl);
?>

 
Best Answer chosen by Ismael Acevedo
Ismael AcevedoIsmael Acevedo
I managed to solve the errors in my code, I leave it in case someone encounters the same problems
 
<?php
$msu ="";
$id=$_POST["id"];

define("SF_SECURITY_TOKEN", "");
define("SF_USERNAME", "");
define("SF_PASSWORD", "");

define("CLIENT_ID", "");
define("CLIENT_SECRET", '');
define("LOGIN_URI", "https://login.salesforce.com");

function loginsale($username, $password) {
  $params = "grant_type=password&client_id=".CLIENT_ID.
           "&client_secret=".CLIENT_SECRET.
           "&username=$username&password=$password";

    $token_url = LOGIN_URI . "/services/oauth2/token";

    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);


     if ( $status != 200 ) {
          die("Error: call to token URL $token_url failed with status $status, curl_errno() ".curl_errno($curl).", curl_error() ".curl_error($curl).", response $json_response");
     }

     return json_decode($json_response, true);

 }

$response = loginsale(SF_USERNAME, SF_PASSWORD);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];

$url = "$instance_url/services/data/v20.0/sobjects/User/$id";

$content = json_encode(array("IsActive" => false));

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Authorization: OAuth $access_token",
            "Content-type: application/json"));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$response = 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));
}

$msu = "Successfully updated user. $id.\n";

// -------------------Send message to the ajax--------------------------
$respon = array('status' => $msu);
echo json_encode($respon);

curl_close($curl);
?>

 

All Answers

Karan Shekhar KaulKaran Shekhar Kaul
Check login history of user to see the error thrown by salesforce
 
Ismael AcevedoIsmael Acevedo
i get a new error 

Error: call to URL https://login.salesforce.com/services/oauth2/token failed with status 400, curl_error , curl_errno 0
Ismael AcevedoIsmael Acevedo
I managed to solve the errors in my code, I leave it in case someone encounters the same problems
 
<?php
$msu ="";
$id=$_POST["id"];

define("SF_SECURITY_TOKEN", "");
define("SF_USERNAME", "");
define("SF_PASSWORD", "");

define("CLIENT_ID", "");
define("CLIENT_SECRET", '');
define("LOGIN_URI", "https://login.salesforce.com");

function loginsale($username, $password) {
  $params = "grant_type=password&client_id=".CLIENT_ID.
           "&client_secret=".CLIENT_SECRET.
           "&username=$username&password=$password";

    $token_url = LOGIN_URI . "/services/oauth2/token";

    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);


     if ( $status != 200 ) {
          die("Error: call to token URL $token_url failed with status $status, curl_errno() ".curl_errno($curl).", curl_error() ".curl_error($curl).", response $json_response");
     }

     return json_decode($json_response, true);

 }

$response = loginsale(SF_USERNAME, SF_PASSWORD);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];

$url = "$instance_url/services/data/v20.0/sobjects/User/$id";

$content = json_encode(array("IsActive" => false));

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
        array("Authorization: OAuth $access_token",
            "Content-type: application/json"));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

$response = 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));
}

$msu = "Successfully updated user. $id.\n";

// -------------------Send message to the ajax--------------------------
$respon = array('status' => $msu);
echo json_encode($respon);

curl_close($curl);
?>

 
This was selected as the best answer