简体   繁体   中英

Hit Api and get Response using http post and curl

Hello every one i am a junior php developer i am working on converting java code to php.. on Java api hit and get response correctly and now i am trying to hit using curl http post in php this is my task in my software house plz help me

i am gonna show you my java code which is correctly working and then my php code which is not working and not parsing params to that api so pls kindly guide me

This is my Java Code This is working correctly i want to do this same work from 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);

               }

            }
}

now i am coding this on php but don't get response check it pls and guide me i am using curl http post and getApiToken method help me to sort out this problem i am very tense.

This is my php code

<?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;
?>

You are posting JSON from your java code. So use json here at PHP as well(make sure the format is ok):

$payload = "{params}";

And the curl options will be

// 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);

Using these, I got sample response:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM