从我的文件中为每个条目执行php代码块

时间:2016-12-20 17:27:24

标签: php foreach

我有文件karte.php,它有以下条目:

  

15274:ol8lcvgqov55q8isk4tgmrifkb
  15274:bgq1ik2e27q100d9kha37clku4
  15274:qfmju9lta7an0f7t1m2d8coker
  15274:pieinji5qinaf6dcbs9fnu50s0
  15274:is9bnc22pss3tv8u3d78pfnbsi
  15274:cg7nt80dgfn39admq4scm5ful0
  15274:kjjn1qpf2hufhiq1uktnk1grc9

我有php代码,它使用curl将DELETE请求发送到url,其中包含票证:15274:cg7nt80dgfn39admq4scm5ful0。 我想为每个条目创建代码,使用相同的变量再次执行代码,例如$ karta。

所以我在cron.php上访问我的脚本 每次执行代码时,我都希望它使用变量$ karta并且每次都使用新值(来自我的php文件的行)。

以下是我目前使用的代码

   /*
  Set the Request Url (without Parameters) here
*/
$api_request_url = 'https://*.*.*/rest/client/users/*/ticket';

/*
  Which Request Method do I want to use ?
  DELETE, GET, POST or PUT
*/
$method_name = 'DELETE';

/*
  Let's set all Request Parameters (api_key, token, user_id, etc)
*/
$api_request_parameters = array();

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

if ($method_name == 'DELETE')
{
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

if ($method_name == 'GET')
{
  $api_request_url .= '?' . http_build_query($api_request_parameters);
}

if ($method_name == 'POST')
{
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

if ($method_name == 'PUT')
{
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($api_request_parameters));
}

/*
  Here you can set the Response Content Type you prefer to get :
  application/json, application/xml, text/html, text/plain, etc
*/

curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: */2.5.0', 'Content-Type: application/json', 'Connection: Keep-Alive', 'Host: *.*.*', 'ticket:'.$karta));

/*
  Let's give the Request Url to Curl
*/
curl_setopt($ch, CURLOPT_URL, $api_request_url);

/*
  Yes we want to get the Response Header
  (it will be mixed with the response body but we'll separate that after)
*/
curl_setopt($ch, CURLOPT_HEADER, TRUE);

/*
  Allows Curl to connect to an API server through HTTPS
*/
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

/*
  Let's get the Response !
*/
$api_response = curl_exec($ch);

/*
  We need to get Curl infos for the header_size and the http_code
*/
$api_response_info = curl_getinfo($ch);

/*
  Don't forget to close Curl
*/
curl_close($ch);

/*
  Here we separate the Response Header from the Response Body
*/
$api_response_header = trim(substr($api_response, 0, $api_response_info['header_size']));
$api_response_body = substr($api_response, $api_response_info['header_size']);

// Response HTTP Status Code
echo $api_response_info['http_code'];

// Response Header
echo $api_response_header;

// Response Body
echo $api_response_body;

1 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西

    $hendl = fopen("karte.php", "r");
if ($hendl) {
    while (($karta = fgets($hendl)) !== false) {
        //Here you enter your code you posted here on Stackoverflow
    }

    fclose($hendl);
} else {
    // error opening the file.
} 

我建议在文件末尾添加此项,因此您不会向目标网址发送重复的删除后请求。

$f = @fopen("karte.php", "r+");
if ($f !== false) {
    ftruncate($f, 0);
    fclose($f);
}