【发布时间】:2012-11-01 16:20:42
【问题描述】:
我需要一些帮助来解决我遇到的这个疯狂问题
我创建了一个类,我使用 Curl 从 URL 获取数据,并使用该信息 $_GET 为每个变量一个值。
所以我有这个类,我正在使用一个新文件来获取发布在新函数中的项目的 $name 但每次我将短语变量放入新函数中时,我都会得到 NULL 这里是我的类-短
class AppStore {
public $name;
public function getData() {
$requestUrl = self::APPSTORE_LOOKUP_URL . $this->appIdentity;
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_REFERER, "");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $this->iTunesUserAgent);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
$parsedResponse = json_decode($response, true);
if ($parsedResponse['resultCount'] == 1)
{
$parsedResponse = $parsedResponse['results'][0];
$this->name = $parsedResponse['trackName'];
require_once('func.ini.php'); // new function to save images etc
} else {
throw new AppStoreService_Exception("mInvalid data returned from the AppStore Web Service. Check your app ID, and verify that the Appstore is currently up.");
}
}
}
// This is the func.ini.php code
echo $parsedResponse['trackName']; // Works here output equals a Name like Baraka etc
function save_image($img){
$dirs = "data/Apps/".$name; // Gets the name of the ID in the appIdentity so we can use CURL to download images and move the images to new folder else it'll fail and won't move
$path = $dirs."ScreenShots";
$fullp = basename($img);
$fullpath = "$path/$fullp";
$something = $dirs.'ScreenShots'
var_dump($parsedResponse['trackName']); // Null same if i echo it i get a white page etc
/* Sample Code
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_URL,$img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
*/
}
基本上,如果您可以查看我是否尝试添加我的类的变量以在新函数中获取 appIdentity 的名称,save_image 它将为空,我不确定如何返回 $ this->name ($parsedResponse['trackName']) 是全局的,供新函数等中的所有使用,因为我的班级使用公共 getData() 然后 $this->name (getData()->name)
这仅适用于任何函数,因此如果有函数,则设置为 name 的变量将仅具有高于函数的值,我希望这是可以理解的。
不确定我是否需要在 $this->name 中返回或什么,因为我尝试在 getName() 下面的类中创建一个新的公共函数,结果也为 null,因此我无法设置a = 语句中的公共变量,因为它会从 try{} 中捕获(e),询问它期望 T_FUNCTION 而不是 T_VARIABLE 即 public $name = $name; // 错误 public $name = 'Test'; // 有效 '{$name}' 无效
class Test {
public $test = 'Help me out please!';
function GetMe() {
echo $this->test;
}
}
$open = new Test;
echo $open->GetMe(); // Outputs "Help me out please"
/* 我不能用变量做到这一点:( 例如 $this->name / $parsedResponse['trackName'] */
如果有人可以帮助我,我将不胜感激
【问题讨论】:
-
您可以使用
save_image($img,$name,$parseReponse)等参数调用新函数 -
之前也尝试过,但我仍然得到 NULL,知道如何在 save_image() 函数中使用它来输出 $this->name 吗?
-
$img的值应该是多少 -
在
class中设置$this-> 名称后,您可以通过在save_image($name_of_class_instance->name)中传递变量或使用global $name_of_class_instance->name启动函数来在其他函数中使用该变量.
标签: php