function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
wixxeywixxey 

Apex Alternative code for PHP

I have and PHP code for accessing the FluidSurvey API. anyone who can give me its APEX alternative or convert the code into apex.. i will be thankful

 

 

<?php
function GET($url){
        global $api_key, $password;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($curl, CURLOPT_USERPWD, $api_key.':'.$password);
        curl_setopt($curl, CURLOPT_SSLVERSION,3);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;
                MSIE 5.01; Windows NT 5.0)");
        curl_setopt($curl, CURLOPT_URL, $url);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
}
Best Answer chosen by Admin (Salesforce Developers) 
gbu.varungbu.varun

Hi

I am giving an example of my running rest API.

I think you are asking for passing authentication variables into URL.

String apiKey = 'apikey';
String pwd = 'pwd';
String u = 'url';

String variables1 = 'Service variables'
String variables2 = 'Service variables'

HttpRequest req = new HttpRequest();

        Http http = new Http();

        HTTPResponse res;

try{
            
req.setEndPoint(u+EncodingUtil.urlEncode(variables, 'UTF-8')+'&variable2='+EncodingUtil.urlEncodevariables2, 'UTF-8'));
req.setMethod('POST');

Blob headerValue = Blob.valueOf(apikey+':'+pwd);

String authorizationHeader = 'Basic '+ EncodingUtil.base64Encode(headerValue);
           
req.setHeader('Authorization', authorizationHeader);
            
req.setHeader('Content-Type', 'application/json');
    
            
             
res = http.send(req);
}
catch(Exception e)P{

 

Regards,

Varun

All Answers

gbu.varungbu.varun

Hi

I am giving an example of my running rest API.

I think you are asking for passing authentication variables into URL.

String apiKey = 'apikey';
String pwd = 'pwd';
String u = 'url';

String variables1 = 'Service variables'
String variables2 = 'Service variables'

HttpRequest req = new HttpRequest();

        Http http = new Http();

        HTTPResponse res;

try{
            
req.setEndPoint(u+EncodingUtil.urlEncode(variables, 'UTF-8')+'&variable2='+EncodingUtil.urlEncodevariables2, 'UTF-8'));
req.setMethod('POST');

Blob headerValue = Blob.valueOf(apikey+':'+pwd);

String authorizationHeader = 'Basic '+ EncodingUtil.base64Encode(headerValue);
           
req.setHeader('Authorization', authorizationHeader);
            
req.setHeader('Content-Type', 'application/json');
    
            
             
res = http.send(req);
}
catch(Exception e)P{

 

Regards,

Varun

This was selected as the best answer
wixxeywixxey

Thanks varun for your reply, it has solved my problem.. so nice of you