0

I'll try again, as my last question wasn't written well enough.

I am writing a PHP script for handling the below JSON HTTP POSTrequest.

{
  "id": "621c46e4-2ca0-4254-a987-fc9e2cc7c552",
  "mobileNumber": 4799999999,
  "price": 20.5,
  "currency": "nok",
  "paymentDate": "2016-08-23T15:35:38.327104+02:00",
  "paymentType": "sms",
  "product": {
    "id": "17",
    "name": "PAPIRFLYET"
  },
  "success": true,
  "accessNumber": 2380,
  "dialog": [
    {
      "date": "2016-08-23T15:35:37.327104+02:00",
      "type": "mo",
      "message": "PAPIRFLYET",
      "consumerCharge": 0
    },
    {
      "date": "2016-08-23T15:35:38.827104+02:00",
      "type": "mt",
      "message": "Takk for din betaling",
      "consumerCharge": 20.5
    }
  ]
}  

I only want to send my self an email with the last 8 digits of mobileNumber.

    $json = file_get_contents("php://input");
    $obj = json_decode($json, true);

    $tlf = substr(strval($obj["mobileNumber"]),2,8);

    $email_from = "xxx@gmail.com";
    $email_to = "xxx@gmail.com";
    $email_subject = "Subject";
    $email_message = "Number is " . $tlf;

    $headers = "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: ' . $email_from . "\r\n";
    @mail($email_to, $email_subject, $email_message, $headers); 

If I just open my browser and go to the page mysite.com/json.php the email is sent (without any number obv), but when the POST request is sent nothing happens.

The client may be requesting a 100-continue, as indicated by the headers (when testing with posttestserver.com):

REQUEST_URI = /post.php
QUERY_STRING = 
REQUEST_METHOD = POST
GATEWAY_INTERFACE = CGI/1.1
REMOTE_PORT = 54820
REMOTE_ADDR = 193.142.108.232
HTTP_CONNECTION = close
HTTP_EXPECT = 100-continue
CONTENT_LENGTH = 638
HTTP_HOST = posttestserver.com
CONTENT_TYPE = application/json
UNIQUE_ID = V7xN1UBaMGUAAC@uOX4AAAAJ
REQUEST_TIME_FLOAT = 1471958485.5507
REQUEST_TIME = 1471958485

Do I need to specifically handle the HTTP reponses in my PHP script?

  • "Nothing happens"? You mean you get a white screen? What debugging have you tried? – Jonnix Aug 23 '16 at 14:34
  • Done ANY basic debugging, like `var_dump($json)` to see if the script received anything? And note that using `@` is the (childish) equivalent of stuffing your fingers in your ears and going "lalalalala can't hear you". If you KNOW the code is not working, then you should NOT be suppressing errors. – Marc B Aug 23 '16 at 14:35
  • @JonStirling I only handle the server side. The `JSON HTTP POST` is sent from a company called ViaNett as a response to receiving an SMS. I only input the http-address they should push to. I have tried pushing to `http://posttestserver.com/post.php` to verify that it is actually pushing, and it is. I also sent to my `json.php` with chromes "Advanced REST client", which works. – Pål Skønberg Løvik Aug 23 '16 at 16:28
  • @MarcB I tried putting in some logging, but it is as if nothing I write in `json.php` is executed. If i send the `HTTP POST` from "Advanced REST Client" it works. – Pål Skønberg Løvik Aug 23 '16 at 16:32
  • then double check that this service's posts are actuallyr eaching your server. There should be hits from them in your server's access log. If there aren't any hits, then the problem isn't with this code, it's with them not hitting the correct url, or not hitting your server at all. – Marc B Aug 23 '16 at 17:01

1 Answers1

0

It seems like the problem was the wordpress hosting. If I put json.phpon mysite.com/json.php it worked fine. mysite.com/wp-content/themes/themeName/json.php did not work. I am not sure why, and neither does google, so I'll leave it at that :)