0

I have following code which returns an empty string (in variable $result)

$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test&mask=1"
curl_setopt($ch, CURLOPT_URL, "$createdURL");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')) ;
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
echo var_dump($result);
if(curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);

Whereas when I run the link given in first line in browser address bar it returns following XML.

<corpsms>
    <command>Submit_SM</command>
    <data>Error 102</data>
    <response>Error</response>
</corpsms>

Please help where am I wrong.

3 Answers 3

1

You are missing curl_init()

So use this code and you'll get a response from the API:

<?php

$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test&mask=1";

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $createdURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')) ;
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
echo var_dump($result);
if(curl_errno($ch)){
        echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);

You should also check whether you need to send the Content-Tye: text/xml header and whether you should be using a POST or a GET.

Sign up to request clarification or add additional context in comments.

1 Comment

$ch = curl_init(); was there but missed while copying.
1

This was due to space given in parameters of link like below

$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test and test&mask=my mask";

After removing space it is now giving output, but now need to tackle matter of spaces.

1 Comment

don't remove the space, urlencode() the variables, see php.net/manual/en/function.urlencode.php
0

You can enable the CURLOPT_VERBOSE option:

curl_setopt($ch, CURLOPT_VERBOSE, true);

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR. The output is very informative.

You can also use tcpdump or wireshark to watch the network traffic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.