Search code examples
phpjsonposthttp-posthttp-status-code-100

Do I need to spesifically handle HTTP reponse in my PHP script that receives JSON HTTP POST?


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 = "[email protected]";
    $email_to = "[email protected]";
    $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?


Solution

  • 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 :)