函数中的PHP curl不返回Antyhing

时间:2018-07-20 08:04:19

标签: php curl php-curl

我有一个需要执行的API URL,它返回某种状态或一个ID,您可以检查该状态并根据该ID获取结果。  我写了一个函数来执行CURL,但是它不返回任何结果。.

    <?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)-不返回任何内容。有人可以详细说明吗?

如果不是在函数内部以curl方式执行-一切正常。

1 个答案:

答案 0 :(得分:0)

问题是传递给函数的错误请求URL。