cURL有替代品吗?

时间:2016-11-11 09:25:39

标签: php curl instamojo

我目前正在使用instamojo支付网关。

我有相同的付款ID,并使用以下代码获取付款状态

echo $cUrl = 'https://test.instamojo.com/api/1.1/payments/' . $payment_id;
                     (function_exists('curl_init')) ? '' : die('cURL Must be installed for geturl function to work. Ask your host to enable it or uncomment extension=php_curl.dll in php.ini');
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $cUrl);
                    curl_setopt($ch, CURLOPT_HEADER, FALSE);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Api-Key:90a1c44nmb8ac4e65nmnm3eab5b308cf",
                                                               "X-Auth-Token:8164083509a889951fhjhjh1ff89c4"));
                    $response = curl_exec($ch);
                    $error_number = curl_errno($ch);
                    $error_message = curl_error($ch);
                    curl_close($ch);
                    $response_obj = json_decode($response, true);
                    echo "Eror number: $error_number<br />";
                    echo "Eror message: $error_message<br />";
                    print_r($response_obj);

                    if($response_obj['success'] == false) {
                        $message = json_encode($response_obj['message']);
                        return Array('payment' => Array('status' => $message));

                    }
                    if(empty($response_obj) || is_null($response_obj)){
                        return Array('payment' => Array('status' => 'No response from the server.'));
                    }
                    else{
                        return $response_obj;
                    }

但是当我在本地主机上运行此代码时,我得到了正确的结果。

但是当我在我的服务器上运行它(即000webhost.com)但收到错误时:

Warning:  curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in /home/a2963438/public_html/wp-content/plugins/instamojo-register/imojoregister_shortcode.php on line 18

我正在使用000webhost.com的免费帐户,因此我无法启用safe_mode或编辑任何php.ini

所以我想问一下,有没有其他方法来实现这个网址的响应?

2 个答案:

答案 0 :(得分:7)

你可能想看看Guzzle。

http://docs.guzzlephp.org/en/latest/

http://docs.guzzlephp.org/en/latest/quickstart.html

根据他们在此处的常见问题解答:http://docs.guzzlephp.org/en/latest/faq.html#does-guzzle-require-curl

Guzzle是否需要cURL?

没有。 Guzzle可以使用任何HTTP处理程序来发送请求。这意味着Guzzle可以与cURL,PHP的流包装器,套接字和非阻塞库(如React)一起使用。您只需要配置HTTP处理程序以使用不同的发送请求的方法。

请注意

Guzzle历史上只使用cURL发送HTTP请求。 cURL是一个了不起的HTTP客户端(可以说是最好的),Guzzle会在默认情况下继续使用它。这种情况很少见,但有些开发人员没有在他们的系统上安装cURL或遇到特定版本的问题。通过允许可交换的HTTP处理程序,Guzzle现在可以更加自定义,并且能够适应更多开发人员的需求。

答案 1 :(得分:1)

最好的方法是升级您的服务器,意味着您可以使用 file_get_contents 来完成任务