dtz33344 2014-05-10 09:40
浏览 49

使用关联数组参数从CURL重新路由AJAX

I am trying to access another site using a POST request through ajax. So the access flow became :

AJAX request -> PHP CURL -> www.somedomain.com

This is the code for the AJAX request. I guarantee it passes the parameters correctly:

$("#new_access_token").submit(function(ev){

$.ajax({
    url : "back_access/access_code.php",
    type : "POST",
    data: "access_token[app_id]=601&access_token[subscriber_num]="+$("#input-phone-number").val(),

    success : function(res){
        console.log(res);
    }
});

return false;
 });

The php curl script is here (access_code.php):

    $ch = curl_init();

$url = "http://developer.globelabs.com.ph/oauth/request_authorization";

$data =  array("access_token" => array(

    'app_id' => $_POST['access_token']['app_id'],
    'subscriber_number' => $_POST['access_token']['subscriber_number']

));

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");   
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

$result = curl_exec($ch);

print_r($result);

curl_close($ch);

It returns an error of "500". With the correct parameters in terminal curl and Advanced Rest Client, it returns the page. However, this script does not. How do I control the parameters?

  • 写回答

1条回答 默认 最新

  • du1913 2014-05-10 09:43
    关注

    because you are posting multidomensional array so You'd have to build the POST string manually, rather than passing the entire array in .. you should add curl header with a form Type multipart and other relative things like accept , content-length etc

    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    

    if you serialize or jsone_encode the whole field you can send the data but in this case you also need to capture the data and unserialize/json_decode it from server end.

    评论

报告相同问题?