PHP 函数中的 curl 没有 return 任何东西
PHP curl in a functions does not return antyhing
我有一个 API url 需要执行,它 return 要么是某物的状态,要么是你可以检查状态并获得结果的 ID在身份证上。
我写了一个函数来执行 CURL,但它没有 return 任何结果..
<?php
function curlCommand($initialCall = false, $URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"API-KEY: keykeykey",
"token: blahlbahlbah"
));
$response = curl_exec($ch);
if($initialCall == false){
$response = (array)json_decode($response);
}else{//initial call. (the response is not json, it's a string incorporated into quotations, we need to take out the quotations and use that response in the url to make a new call.)
$response = str_replace('"','',$response);
}
curl_close($ch);
return $response;
}
$init = curlCommand(true,"https://url.com/command/");
echo "init: ";
print_r($init);
echo "\n";
print_r($init) - 没有 return 任何东西。有人可以详细说明吗?
如果作为 curl 不在函数内部执行 - 一切正常。
问题是传递到函数中的错误请求 URL。
我有一个 API url 需要执行,它 return 要么是某物的状态,要么是你可以检查状态并获得结果的 ID在身份证上。 我写了一个函数来执行 CURL,但它没有 return 任何结果..
<?php
function curlCommand($initialCall = false, $URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"API-KEY: keykeykey",
"token: blahlbahlbah"
));
$response = curl_exec($ch);
if($initialCall == false){
$response = (array)json_decode($response);
}else{//initial call. (the response is not json, it's a string incorporated into quotations, we need to take out the quotations and use that response in the url to make a new call.)
$response = str_replace('"','',$response);
}
curl_close($ch);
return $response;
}
$init = curlCommand(true,"https://url.com/command/");
echo "init: ";
print_r($init);
echo "\n";
print_r($init) - 没有 return 任何东西。有人可以详细说明吗?
如果作为 curl 不在函数内部执行 - 一切正常。
问题是传递到函数中的错误请求 URL。