HTTP POST PHP

Hi,

I have this Java HTTP code that is connected to Event Store and working fine.

Now I’m trying to translate this Java HTTP code to HTTP POST PHP code.

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.HttpParams;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import org.json.JSONString;

import org.json.simple.parser.JSONParser;

import org.json.simple.parser.ParseException;

public class TestPost {

public static void main(String[] args) throws ClientProtocolException, IOException, JSONException, ParseException {

HttpClient client = new DefaultHttpClient();

HttpPost post = new HttpPost(“http://127.0.0.1:2113/streams/newstreamarna”);

post.addHeader(“content-type”, “application/json”);

// post.addHeader(“ES-EventType”, “SomeEvent”);

JSONObject jo = new JSONObject();

jo.put(“eventId”, “fbf4a1a1-b4a3-4dfe-a01f-ec52c34e16e4”);

jo.put(“eventType”, “event-type”);

jo.put(“data”, “{ a : 2 }”);

jo.put(“metadata”, “{ a : 1 }”);

JSONArray ja = new JSONArray();

ja.put(jo);

StringEntity input = new StringEntity(ja.toString());

post.setEntity(input);

HttpResponse response = client.execute(post);

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

String line = “”;

while ((line = rd.readLine()) != null) {

System.out.println(line);

}

}

}

I have tried to use this PHP code:

$url = '[http://127.0.0.1:2113/streams/newstreamarna](http://127.0.0.1:2113/streams/newstreamarna)';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

But when I use this PHP code i get an error on the HTML site, the code is shown on the HTML site, see picture.

It doesn’t work, what do I do wrong? Is the code on the wrong place? Can you give me any advice if there is a easier way to do HTTP POST PHP that can connect to Event Store?

Here is the full HTML code:

Din Mat

Din Mat

Startsida

Kryssa i en av varje kategori

Måltid

Frukost

Lunch

Middag

Mellanmål

Dessert

Ursprung

Afrika

Asien

Europa

Nordamerika

Sydamerika

Tid

15 min

30 min

60 min

120 min

120+ min










<?php if (isset($_GET['todo'])) { $maltidid = $_GET['maltidid']; $ursprungid= $_GET['ursprungid']; $tidid= $_GET['tidid']; $dbhost = 'localhost'; $dbuser = '***'*; $dbpass = '****'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } $sql = 'SELECT beskriv FROM recept WHERE maltidid='.$maltidid.' AND ursprungid='.$ursprungid.' AND tidid='.$tidid; mysql_select_db('m11p0753'); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not get data: ' . mysql_error()); } while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) { echo ""; $sEncoding = mb_detect_encoding($row['beskriv']); $str = mb_convert_encoding($row['beskriv'], 'Windows-1252', 'UTF-8'); echo $str; echo "
"; } mysql_close($conn); $url = '[http://127.0.0.1:2113/streams/newstreamarna](http://127.0.0.1:2113/streams/newstreamarna)'; $data = array('key1' => 'value1', 'key2' => 'value2'); // use key 'http' even if you send the request to https://... $options = array( 'http' => array( 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($data), ), ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); var_dump($result); } ?>

Copyright (c) 2014 by Din Mat

Thankful for any help I get, Cheers!

bild1.png