通过获取卷曲的curl_exec一个API的回应()而不是通常的回调文件 [英] Getting an API's response through curl's curl_exec() instead of the usual callback file

查看:109
本文介绍了通过获取卷曲的curl_exec一个API的回应()而不是通常的回调文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API集成说明结果
该API需要被张贴到一些输入字段和客户令牌API URL的形式。该API过程再上岗回应我的服务器上callback.php文件(它被固定为每个文件,我们可以不提,我们要响应被张贴到其他一些文件)。我可以访问该文件使用$ _ POST张贴丘壑。这是所有关于现有的方法,它工作正常。

现在,我想知道是否有可能打通卷曲的curl_exec()函数的返回值,整个响应?我不跟卷曲经验。

我执行以下code: -

  $ CH = curl_init(API_URL);
$连接codeD ='';
$ _postArray ['customer_token'] = API_CUSTOMER_TOKEN;的foreach($ _ postArray为$名=> $值)
{
     。$连接codeD = urlen code($名)'='urlen code($值)。'和;';
}//砍掉最后的符号
$连接codeD = SUBSTR($连接codeD,0,strlen的($连接codeD)-1);
curl_setopt($ CH,CURLOPT_HEADER,0);
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ CH,CURLOPT_POSTFIELDS,$连接codeD);
$ RESP = curl_exec($ CH);
curl_close($ CH);
回声$ RESP;

有没有反应,但如果我注释掉 curl_setopt($ CH,CURLOPT_RETURNTRANSFER,1); 线,$ RESP值相呼应,1而不是数组($ _ POST)就像我的预期。这1是否意味着已检测到的API,它成功的处理?

如果这是不可能的,告诉我如何修改上面至少可以得到在callback.php文件$ _ POST响应就像是早期的发生。

的现状更多的描述和我想要的东西结果
我希望得到的响应,curl_exec(的返回值),这样我可以用ajax响应前端页面重定向到成功页面(我想通过ajax的表单提交从前端保持在同一页面中调用卷曲发布显示直到API响应来)。

目前我是通过一个隐藏的iframe发布API的形式做这个(保持显示相同的页面),开始一个JavaScript计数器检测会话['api_success']变量,以及设置会话['api_success']在callback.php文件(从后成功API响应)。我想丢弃方法,因为它可以打破任何时间,它在Linux的Firefox 2。

失败

更新结果
一致认为,这种异步API的,没有别的选择,只能做频繁的轮询设置的状态标志。所以,请帮我改变上述code,以便接收回调文件的响应。目前,它不来了。

谢谢,
Sandeepan


解决方案

  

现在,我想知道是否有可能打通卷曲的curl_exec()函数的返回值?

整个响应

我不知道你在说什么了API,但最有可能没有。该API可能会启动一个后台进程时,它的完成触发回调。最初的请求不会在这种设置中收到一个返回值。

API integration description
The API needs a form to be posted to the API URL with some input fields and a customer token. The API processes and then posts response to a callback.php file on my server (it is fixed as per the documentation and we cannot mention that we want the response to be posted to some other file). I can access the posted vals using $_POST in that file. That's all about the existing method and it works fine.

Now, I want to know if it is possible to get the entire response through curl as a return value of the curl_exec() function?. I am not experienced with curl.

I executed the following code :-

$ch = curl_init(API_URL);
$encoded = '';
$_postArray['customer_token'] = API_CUSTOMER_TOKEN;

foreach($_postArray as $name => $value) 
{
     $encoded .= urlencode($name).'='.urlencode($value).'&';
}

// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
$resp = curl_exec($ch);
curl_close($ch);
echo $resp;

There is no response but if I comment out the curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); line, the $resp value echoes 1 instead of an array ($_POST) like I expected. Does this 1 mean that succesful handling by API has been detected?

If this is not possible, tell me how to modify the above to at least get the response in the callback.php file as $_POST like it was happening earlier.

More description of Current status and what I wanted
I wanted to get the response as a return value of curl_exec() so that I could use ajax response to redirect the page in frontend to a success page (I wanted to call curl posting through ajax on form submission from frontend keeping the same page in display until response from API comes).

Currently I was doing this by posting the API form through a hidden iframe(to keep displaying the same page), starting a javascript counter to detect a session['api_success'] variable, and setting the session['api_success'] in the callback.php file (after successful response from API). I wanted to discard that method as it can break any time and it fails in linux firefox 2.

Updates
Agreed that with this kind of asynchronous API, there is no other option but to do frequent polling to set a status flag. So, please help me alter the above code so as to receive response in the callback file. Currently it is not coming.

Thanks, Sandeepan

解决方案

Now, I want to know if it is possible to get the entire response through curl as a return value of the curl_exec() function?

I don't know the API you're talking about, but most probably no. The API will likely start a background process that triggers the callback when it's done. The original request does not receive a return value in this kind of setup.

这篇关于通过获取卷曲的curl_exec一个API的回应()而不是通常的回调文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆