Hello,
I am trying to use an API offered by a website in PHP however I am facing problems. I can use the API very fine in C#. The following code in C3 works:
string result = wb.UploadString(apiurl, "POST", "Hello, where do you live");
MessageBox.Show(result);
The above code works perfectly find. But I want to make the same code in PHP. In PHP I am utilizing HTTP POST from PHP without CURL , I have tried with CURL as well but it just doesn't work, I do not get anything returned from the API.
The following is the PHP code:
function do_post_request($url, $data, $optional_headers = null)
{
$params = array('http' => array('method' => 'POST','content' => $data));
if ($optional_headers !== null)
{
$params['http']['header'] = $optional_headers;
}
$ctx = stream_context_create($params);
echo $ctx['content'];
$fp = @fopen($url, 'rb', false, $ctx);
if (!$fp)
{
throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false)
{
throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;
}
$result = do_post_request($apiurl,$txtstr);
echo $result;
I get no result, Neither do I get any error.
The API has strict condition that:
you should put your content as the POST DATA, and don’t write any other content or parameters in the POST DATA
Any one have any idea and can help me?
Regards