简体   繁体   English

在PHP中使用参数调用外部网址

[英]Call external url with parameter in PHP

I have been working to call external url from PHP (on submit) with parameter. 我一直在努力从PHP(在提交时)使用参数调用外部url。 I have looked different option ( curl , file_get_contents etc), but nothing seems to working. 我看过不同的选项( curlfile_get_contents等),但似乎没有任何作用。

This is how final url should look like: 最终网址应如下所示:

http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=temp&memory_mb=1024&num_cpus=1&eth0_ip=172.XX.XX.XXX&eth1_ip=192.XX.XX.XX

And with parameter it looks this way: 并带有参数,它看起来是这样的:

http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=$login&memory_mb=$memory&num_cpus=$cpu&eth0_ip=$ip_172&eth1_ip=$ip_192";

You did not escaped the variables you used while creating the URL. 您没有对创建URL时使用的变量进行转义。 If you placed variables in a string, you need to tell, what a variable is and what the string is. 如果将变量放在字符串中,则需要告诉变量是什么以及字符串是什么。

For example, you could use bracers around the varibales: 例如,您可以在各种方括号周围使用护腕:

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user={$login}&memory_mb={$memory}&num_cpus={$cpu}&eth0_ip={$ip_172}&eth1_ip={$ip_192}";

Or exclude them from the string: 或从字符串中排除它们:

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=" . $login . "&memory_mb=" . $memory . "&num_cpus=" . $cpu . "&eth0_ip=" . $ip_172 . "&eth1_ip=" . $ip_192;

You have to use the correct quotation characters " instead of ' around the string or you might concat the parameters/strings using a . 您必须在字符串周围使用正确的引号字符"而不是' ,否则您可以使用来连接参数/字符串.

A possible solution is: 可能的解决方案是:

$url = 'http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user='.$login.'&memory_mb='.$memory.'&num_cpus='.$cpu.'&eth0_ip='.$ip_172.'&eth1_ip='.$ip_192;

Build your url as a string then use file_get_contents() 将您的网址构建为字符串,然后使用file_get_contents()

$login = "userName";
$memory = "memoryValue";
$cpu = "cpuValue";
$ip = "127_0_100_"; 

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=" . $login . "&memory_mb=" . $memory . "&num_cpus=" . $cpu . "&eth0_ip=" . $ip . "_172&eth1_ip=" . $ip . "_192";

$content = file_get_contents($url);

Build your url with all define param as a string then use file_get_contents() or curl() . 使用所有定义参数作为字符串构建url,然后使用file_get_contents()curl()

$url = "http://XXXXX:8080/job/Clone_CentOS_VM/buildWithParameters?token=smlvyKf6tS&vm_user=".$login."&memory_mb=".$memory."&num_cpus=".$cpu."&eth0_ip=".$ip_172."&eth1_ip=".$ip_192;
  1. with file_get_contents() : file_get_contents()

    file_get_contents($url); file_get_contents($ url);

  2. with curl() curl()

      $request_headers[] = 'Content-Type:application/json'; if (!function_exists('curl_init')){ die('cURL is not installed. Install and try again.'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $result = curl_exec($ch); 

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

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