1

I am trying to add a new Remote Site Setting via API. I am using core PHP. After I run the following code I am getting this error.

$response = [
   0 => {
   "errorCode": "NOT_FOUND"
   "message": "The requested resource does not exist"
  }
]

It seems I am using wrong end point. Can anyone guide me what are the end points for add new remote site?

OR

if I can't use rest api then what will be the soap envelope and soap end URL

Please Help.

Here is my code.

$url = $instance_url . '/services/data/v53.0/metadata/RemoteSiteSetting'; 
//RemoteProxy, RemoteSiteSetting
    $data = [
        'fullName'  => 'My First Hook', 
        'isActive'  =>  true,
        'url' => 'http://www.myServerUrl.com',
        'description' => 'Used for Apex callout to mapping web service for my server'
    ];
$result = $this->callAPI('POST', $url, $data, $access_token, ['accountId' => $accountId]);


public function callAPI($method = 'GET', $url = '', $data = [], $access_token = '', $extras = []) {
    $header = [
        'Authorization: Bearer ' . $access_token,
        'X-PrettyPrint:1',
        'Content-Type: application/json'
    ];
   

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
        case "GET":
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "POST":
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
            break;
        case "DELETE":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            break;
        case "PATCH":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
            break;
    }
    $response = curl_exec($curl);
    $response_object = json_decode($response);
    return $response_object;
}
1
  • Best practice is to use Named Credentials for callouts...even if there are no credentials managed by/stored in NC. Doing so automatically whitelists the target URL.
    – identigral
    Jun 17, 2022 at 17:56

1 Answer 1

0

I have found the solution and posting here. Maybe someone need this. Here is my php code.

$data = [
        'fullName'  => 'HookName',
        'isActive'  =>  true,
        'url' => 'https://www.mysite.com',
        'description' => 'Used for Apex callout to mapping web service for my server'
    ];
    $access_token = $tokens['access_token'];
    $url = 'https://login.salesforce.com/services/oauth2/userinfo';
    $loginUserInfo = $this->callAPI('GET', $url , [], $tokens['access_token'], ['accountId' => $accountId]);
    $post_organization_id = '';
    if($loginUserInfo) {
       
        $organization_id = $loginUserInfo->organization_id;
        $post_organization_id = substr($organization_id, 0, -3);
    }
    
    
    $wsdl = '<?xml version="1.0" encoding="utf-8"?>';
    $wsdl .=        '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
    $wsdl .=            '<env:Header>';
    $wsdl .=                '<urn:SessionHeader xmlns:urn="http://soap.sforce.com/2006/04/metadata">';
    $wsdl .=                    '<urn:sessionId>' . $access_token . '</urn:sessionId>';
    $wsdl .=                '</urn:SessionHeader>';
    $wsdl .=            '</env:Header>';
    $wsdl .=            '<env:Body>';
    $wsdl .=                '<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">';
    $wsdl .=                    '<metadata xsi:type="RemoteSiteSetting">';
    $wsdl .=                        '<fullName>' . $data['fullName'] . '</fullName>';
    $wsdl .=                        '<description>'.$data['description'].'</description>';
    $wsdl .=                        '<disableProtocolSecurity>false</disableProtocolSecurity>';
    $wsdl .=                        '<isActive>true</isActive>';
    $wsdl .=                        '<url>' . $data['url'] . '</url>';
    $wsdl .=                    '</metadata>';
    $wsdl .=                '</createMetadata>';
    $wsdl .=            '</env:Body>';
    $wsdl .=        '</env:Envelope>';
    

    $xml_post_string = $wsdl;

    $headers = array(
        "Content-type: text/xml;charset=\"utf-8\"",
        "Accept: text/xml",
        "Cache-Control: no-cache",
        "Pragma: no-cache",
        "SOAPAction: RemoteSiteSetting",
        "Content-length: " . strlen($xml_post_string),
        'Authorization: Bearer ' . $access_token,
    );

    $url =  'https://yourSubDomain.salesforce.com/services/Soap/m/49.0/'.$post_organization_id;

    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

  
    $response = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.