简体   繁体   English

cURL返回一个空字符串

[英]cURL returns an empty string

I have following code which returns an empty string (in variable $result) 我有以下代码返回一个空字符串(在变量$ 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. 而当我运行浏览器地址栏中第一行中给出的链接时,它将返回以下XML。

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

Please help where am I wrong. 请帮我哪里错了。

You are missing curl_init() 您缺少curl_init()

So use this code and you'll get a response from the API: 因此,使用此代码,您将从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 . 您还应该检查是否需要发送Content-Tye: text/xml标头,以及是否应该使用POSTGET

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 "; $ createdURL =“ https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text= 测试并测试 &mask = 我的面具 ”;

After removing space it is now giving output, but now need to tackle matter of spaces. 删除空间后,它现在提供输出,但是现在需要解决空间问题。

You can enable the CURLOPT_VERBOSE option: 您可以启用CURLOPT_VERBOSE选项:

curl_setopt($ch, CURLOPT_VERBOSE, true);

When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR . CURLOPT_VERBOSE ,输出将写入STDERR或使用CURLOPT_STDERR指定的文件。 The output is very informative. 输出非常有用。

You can also use tcpdump or wireshark to watch the network traffic. 您还可以使用tcpdump或wirehark来监视网络流量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM