你好, 我正在尝试使用PHP网站提供的API,但是我遇到了问题.我可以在C#中很好地使用API. C3中的以下代码有效:
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);上面的代码可以完美地找到.但是我想用PHP编写相同的代码.在PHP中,我正在使用不带CURL的PHP中的HTTP POST,我也尝试过使用CURL,但是它不起作用,我没有从API返回任何信息. 以下是PHP代码:
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;我没有结果,也没有任何错误. 该API的严格条件是: 您应该将内容作为POST DATA,并且不要在POST DATA中写任何其他内容或参数 任何人都有什么想法可以帮助我吗? 关于
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
推荐答案url,
数据, data,
optional_headers = null) { optional_headers = null) {
更多推荐
简单的PHP发布问题
发布评论