【问题标题】:Hit Api and get Response using http post and curl点击 Api 并使用 http post 和 curl 获取响应
【发布时间】:2014-03-20 06:26:25
【问题描述】:

大家好,我是一名初级 php 开发人员,我正在努力将 java 代码转换为 php.. 在 Java api 命中并正确获得响应,现在我正在尝试在 php 中使用 curl http post 这是我的任务软件公司请帮助我

我将向您展示我的正确工作的 java 代码,然后是我的 php 代码,该代码不工作并且没有将参数解析为该 api,所以请指导我

这是我的 Java 代码 这工作正常我想从 php 做同样的工作

import java.io.*;

import java.util.jar.JarException;
import org.apache.http.HttpResponse;
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.json.*;

class MyCode{

            public static void main(String[] args) throws JarException, JSONException
            {

                        testCustomerApiIsExposed();

            }


            public static void testCustomerApiIsExposed() throws JarException, JSONException {

               try {

                   @SuppressWarnings("deprecation")

                   HttpClient c = new DefaultHttpClient();

                   HttpPost p = new
                   HttpPost("http://link");



                    String payload = "{id:\"" + 1 + "\","  + "method:\"" + "customerApi.getApiToken" + "\", params:[\"teabonezenminddemo1partner@gmail.com\", \"demo1234!\", \"\", \"\", \"\",     \"\", \"\", false, \"\", \"\"" + "]}";

                   String mimeType="";

                   /*There is something here. What constructor are we really calling here? */

                   // p.setEntity(new StringEntity( payload,ContentType.create("application/json")));

                                    p.setEntity(new StringEntity(payload));



                   HttpResponse r = c.execute(p);



                   BufferedReader reader = new BufferedReader(new InputStreamReader(r.getEntity().getContent(), "UTF-8"));

                   StringBuilder builder = new StringBuilder();

                   for (String line = null; (line = reader.readLine()) != null;) {

                       builder.append(line).append("\n");

                   }

                   JSONTokener tokener = new JSONTokener("[" + builder.toString() + "]");

                   JSONArray finalResult = new JSONArray(tokener);

                   JSONObject o = finalResult.getJSONObject(0);


                   //Getting names of the JSON object here
                   System.out.println(o.names());

                   String apiToken = (String) o.get("result");


                   System.out.println(apiToken);

               }

               catch(IOException e) {

                   System.out.println(e);

               }

            }
}

现在我在 php 上对此进行编码,但没有得到响应,请检查并指导我,我正在使用 curl http post 和 getApiToken 方法帮助我解决这个问题,我很紧张。

这是我的 php 代码

<?php

$data = array(params);

$ch = curl_init('http://link');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
echo $result;
?>

【问题讨论】:

  • 您得到什么错误响应?我收到 404 page not found 错误!

标签: java php http curl


【解决方案1】:

您正在从您的 java 代码中发布 JSON。所以在 PHP 中也使用 json(确保格式正确):

$payload = "{params}";

卷曲选项将是

// as you are posting JSON, so tell server that you are sending json
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));

// Let server know that you are doing HTTP POST request
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

使用这些,我得到了示例响应:

{"id":"1","result":"results"}

【讨论】:

    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 2015-05-21
    • 2011-08-13
    • 2015-05-28
    • 2014-05-17
    相关资源
    最近更新 更多