2014-10-08 93 views
1

所以我想在这里做一个小的API。我只会通过头部发送一些信息(得到参数),我没有使用任何POST参数。 这是我写的代码。只需发送变量通过GET在cURL

function sendSMS($message, $number) 
{ 
    /*** Connection Params **/ 
    /*** Build the request parameters ***/ 
    $service="sms_api_call_receiver.php"; 
    $number="1212"; 
    $message="asas"; 
    $result = sendPost("https://www.domain.com/smsapp/" . $service . "?message=".urlencode($message)."&number=".$number); 
    return $result; 
}//function 

function sendPost($Url) 
{ 
    // Initialisation 
    $ch=curl_init(); 
    // Set parameters 
    curl_setopt($ch, CURLOPT_URL, $Url); 
    // Return a variable instead of posting it directly 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    // Active the POST method 
    //curl_setopt($ch, CURLOPT_POST, 1) ; 
    // Request 
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $strRequest); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    // execute the connexion 
    $result = curl_exec($ch); 
    // Close it 
    //return curl_error($ch); 
    curl_close($ch); 
    return $result; 
} 

在接收文件,我有这样的代码: -

if(isset($_GET['message'])) 
{ 
    return true; 
} 
else 
{ 
    return "12212"; 
} 

但是当我测试,输出我得到的是: -

string '' (length=0) 

我在做什么错在这里?

疑难解答 我试着看看curl_error是否返回任何东西。但我什么都看不到。 这里的任何建议都会有所帮助。

+1

'的var_dump($ _ GET)',确保这些参数通过在所有实际的到来。 – 2014-10-08 18:22:01

回答

2

您确定有问题吗?如果$_GET['message']已设置(在您的测试示例中),脚本(sms_api_call_receiver.php)返回true并停止执行。

因为它是HTTP请求,并且响应是空的,所以会得到sendSMS()函数字符串中长度为0的结果。

+0

好的,如果我返回类似“return”ok“'的东西而不是返回true。我会变好,而不是0长度的字符串? – user2619381 2014-10-08 18:23:47

+0

当然。无论您在那里打印什么,都会传回并由'sendSMS()'返回。正确的方法是'echo“ok”;'。 – 2014-10-08 18:24:54

1

得到的东西作为回应在sendSMS你需要sms_api_call_receiver.php打印而不是使用回