1

i have a register page for allow users to register. before register i need to validate their phone number. i have given a web-service address along with its parameters. the parameters i have given:

http://*********
Method:POST
Headers:Content-Type:application/json
Body:
the following in:
{
    "mobileNo":"0*********",
    "service":"****",
    "Code1":"*****",
    "content":"hi",
    "actionDate":"2017/09/26",
    "requestId":"1"
            }

and here the code i found in the Internet:

$data = array(
  'mobileNo'      => '****',
  'service'    => '***',
  'Code1'       => '*****',
  'content' => '55',
  'actionDate'      => '2017/09/26');

$options = array(
'http' => array(
'method'  => 'POST',
'content' => json_encode( $data ),
'header'=>  "Content-Type: application/json" .
            "Accept: application/json"
)
);
$url =  "******";
$context  = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$response = json_decode( $result );

and here is error i face with when i test local:

 file_get_contents(http://********/sms-gateway/sms-external-zone /receive): failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

and there is no error and no result(receive SMS) in response when i test online(cpanel server)

According to the given parameters, where do i wrong?

thanks in advance.

shadow
  • 29
  • 1
  • 7

1 Answers1

0

According to your Error, it seems your service did not respond. Have you tried to open it in a browser, to check if any response there?

Maybe the service you try to call requires you to provide a Static IP from your Webserver, as they only grant access on a IP based level. Means, your IP is blocked until they allow it.

I suggest you use cURL to do your request. This way you get future data to use for debugging, if anything fails. Still here, if the service does not respond, you want get any other information.

$data = array(
  'mobileNo'      => '****',
  'service'    => '***',
  'Code1'       => '*****',
  'content' => '55',
  'actionDate'      => '2017/09/26');
$url =  "******";

$ch = curl_init( $url );
// set data as json string
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data));
// define json as content type
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// tell curl to fetch return data
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// follow location if redirect happens like http to https
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
// send request
$result = curl_exec($ch);

// gives you the result - most of the time you only want this
var_dump($result);

// for debugging purpose, gives you the whole connection info
var_dump(curl_getinfo($ch));

// gives back any occurred errors
var_dump(curl_error($ch));

curl_close($ch);

Edit: I added the CURLOPT_FOLLOWLOCATION, as a request may gets redirected. We want to catch that as well. And I added the curl_close at the end. If it is closed, error or info data can be fetched.

S.Gartmeier
  • 476
  • 5
  • 14
  • tanks for your response. my ip was blocked and i ask them to fix this. when i use your suggested code, i face with this error: bool(false) bool(false) bool(false) – shadow Sep 27 '17 at 11:04
  • and when i use my own code nothing happens. no error no result – shadow Sep 27 '17 at 11:15
  • @shadow I just updated my code. Problem was, curl_close was called before any curl_error or curl_getinfo. Just corrected that. I did also add the CURLOPT_FOLLOWLOCATION option, that gives curl the ability to follow any redirects. – S.Gartmeier Sep 27 '17 at 11:29
  • i have one more question. at the other side, how to read this parameters and store it in a variable – shadow Sep 28 '17 at 14:40
  • i had used: $mobNo = $_POST['mobileNo']; but the variable $mobNo is empty – shadow Sep 28 '17 at 14:42
  • @shadow Try `var_dump($_POST);` to see what your Postdata includes. You may want to check what your html form does. Is it set to POST, how are the fields names? This will most likely help you find your problem. – S.Gartmeier Sep 28 '17 at 14:58
  • i mean this data: **json_encode($data)** that sent to the url. how to receive this otherside – shadow Sep 28 '17 at 15:22
  • @shadow I am not quite sure what you mean. Do you mean the response or your data setup, to fill that array? – S.Gartmeier Sep 28 '17 at 15:25
  • this question is completely different. the code you update it before works fine now. i want to receive this encoded json data at other side(example: at example.com/webservice). and do users request. i mean receive mobile number, content, service code.... and send the sms and store information into database. sorry if the text is long:-) – shadow Sep 28 '17 at 15:36
  • @shadow In this case, this question that has been asked before will suit you: https://stackoverflow.com/questions/14914628/php-decode-json-post – S.Gartmeier Sep 28 '17 at 21:23