Asif Waheed
Asif Waheed

Reputation: 73

cURL returns an empty string

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.

Upvotes: 0

Views: 4316

Answers (3)

Asif Waheed
Asif Waheed

Reputation: 73

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.

Upvotes: 1

Chris
Chris

Reputation: 4728

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.

Upvotes: 1

user3284065
user3284065

Reputation: 1

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.

Upvotes: 0

Related Questions