简体   繁体   English

我如何有一个公共函数来运行所有PHP cURL请求?

[英]How can I have a public function to run all my PHP cURL requests?

I have a basic RESTful API setup using cURL for all my requests. 我有一个基本的RESTful API设置,所有请求都使用cURL。

At the moment, I am attempting to split out all my functions to have one function that runs all my requests. 目前,我正尝试将我的所有功能拆分为一个可以运行所有请求的功能。

For example, I have the following which can be called from localhost/api/getUserData 例如,我有以下内容可以从localhost/api/getUserData调用

private function getUserData() {
     $url = 'localhost/api/users/';
     runApiCall($url);
}

That function then goes on to pass the URL to runApiCall() , which is the following: 然后,该函数继续将URL传递给runApiCall() ,如下所示:

function runApiCall($url) {

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);
     $result = curl_exec($ch);
     curl_close($ch);

     $this->response($result, 200);
}

Although, I keep getting an error in my console of http://localhost/api/getUserData 500 (Internal Server Error) 虽然,我的控制台中http://localhost/api/getUserData 500 (Internal Server Error)出现错误http://localhost/api/getUserData 500 (Internal Server Error)

The API was working perfectly fine before I split it out. 在拆分之前,API的运行情况非常好。

EDIT: 编辑:

If I simply change my getUserData() function to the following, it works perfectly fine: 如果我只是简单地将getUserData()函数更改为以下内容,则可以正常运行:

private function getUserData() {
     $url = 'localhost/api/users/';

     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID=' . $_COOKIE['PHPSESSID']);

     $result = curl_exec($ch);
     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     $this->response($this->json($result), $status);
}

You have an issue with scope as you are inside a class. 在类内部时,范围存在问题。

private function getUserData() {
     $url = 'localhost/api/users/';
     $this->runApiCall($url); // call $this object's runAppCall() function
}

If you had error handling enabled, you'd see an error thrown by PHP telling you that it could not find the method runApiCall() . 如果启用了错误处理,则会看到PHP抛出一个错误,告诉您它找不到方法runApiCall() This is because you are inside a class and must explicitly tell PHP to run the method on the class. 这是因为您在一个类中,并且必须显式告诉PHP在该类上运行该方法。

You can do this by: 您可以通过以下方式做到这一点:

private function getUserData() {
     $url = 'localhost/api/users/';

     // EITHER
     $this->runApiCall($url); 

     // OR
     self::runApiCall($url);
}

In future, I'd recommend adding this line at the route of your application when you are developing, then set it to false in production: 以后,建议您在开发时在应用程序的路线上添加以下行,然后在生产中将其设置为false:

ini_set('display_errors', 1);

This will now log any PHP errors to the browser, making it much easier for you to debug. 现在,这会将所有PHP错误记录到浏览器中,从而使调试变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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