Jaykishan Solanki
Jaykishan Solanki

Reputation: 63

Pass Params in Curl Request

I have try curl request with terminal it's working but when i convert that curl request into php code that one passing param not working.

Terminal curl request :

curl --insecure "https://www.zohoapis.in/phonebridge/v3/clicktodial" -X POST -d "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456" -H "Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf" -H "Content-Type: application/x-www-form-urlencoded"

Response :

{"message":"ASTPP Clicktodial functionality has been enabled","status":"success","code":"SUCCESS"}

PHP Code :

$zohouser = '6000';

            $access_token = '1000.c3c1107b635f1f5b257d831677e077d2';
            $cURL = "https://www.zohoapis.in/phonebridge/v3/clicktodial?clicktodialuri=$click_to_dial&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=$zohouser";

            
            $curl = curl_init();

            curl_setopt_array($curl, array(
                CURLOPT_URL => $cURL,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_HTTPHEADER => array(
                "Authorization: Zoho-oauthtoken " . $access_token,
                "Content-Type: application/x-www-form-urlencoded",
                "cache-control: no-cache"
                ),
            ));

            $response = json_decode(curl_exec($curl));

            $err = curl_error($curl);
print_r($err);
            curl_close($curl);
print_r($response);exit;

Not getting any response or error during run this php curl request. Can you please help me how to pass string as param in php curl request.

Upvotes: 3

Views: 369

Answers (2)

Pratik Patel
Pratik Patel

Reputation: 239

$strURL= "https://www.zohoapis.in/phonebridge/v3/clicktodial";


$arrHeader= array(
    'Authorization:Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf'
 );

 $params= array(
     
"clicktodialparam"=>"[{\"name\":\"fromnumber\",\"value\":\"555\"}]",
"authorizationparam"=>"{\"name\":\"X-Auth-Token\",\"value\":\"1000.aedb399e2389cfacef60f965af052cbf\"}",
"clicktodialuri" => "$click_to_dial",
"zohouser" => "123456"



);


$ch= curl_init();

curl_setopt_array($ch,array(
    CURLOPT_URL =>$strURL,
    CURLOPT_POST => 1,
    CURLOPT_HTTPHEADER => $arrHeader,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CONNECTTIMEOUT => 0,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_POSTFIELDS => http_build_query($params)
    

));


$strResponse= curl_exec($ch);

print_r($strResponse);

echo curl_error($ch);

Upvotes: 1

JerodG
JerodG

Reputation: 1334

Referring to this post and using the linked tool we end up with the following code. Since I cannot test this myself I cannot guarantee my answer. It looks like you might be missing the curl post option "curl_setopt($ch, CURLOPT_POST, 1);" which you can use in lieu of the option you used "CURLOPT_CUSTOMREQUEST => "POST"".

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.zohoapis.in/phonebridge/v3/clicktodial');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "clicktodialuri=$clicktodialurl&clicktodialparam=[{'name':'fromnumber','value':'555'}]&zohouser=123456");

$headers = array();
$headers[] = 'Authorization: Zoho-oauthtoken 1000.aedb399e2389cfacef60f965af052cbf';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Upvotes: 0

Related Questions