php 如何使用 curl GET 而不是 POST

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15486988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:19:32  来源:igfitidea点击:

How to use curl GET instead of POST

phpcurl

提问by Plummer

I'm tyring to use curl to print a return from a url. The code I have so far looks like this:

我很想使用 curl 来打印来自 url 的返回值。到目前为止,我的代码如下所示:

<?php
    $street = $_GET['street'];
    $city = $_GET['city'];
    $state = $_GET['state'];
    $zip = $_GET['zip'];

    $url = 'http://eligibility.cert.sc.egov.usda.gov/eligibility/eligibilityservice';
    $query = 'eligibilityType=Property&requestString=<?xml version="1.0"?><Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd"><PropertyRequest StreetAddress1="'.$street.'" StreetAddress2="" StreetAddress3="" City="'.$city.'" State="'.$state.'" County="" Zip="'.$zip.'" Program="RBS"></PropertyRequest></Eligibility>';
    $url_final = $url.''.$url_query;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$query);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $return = curl_exec ($ch);
    curl_close ($ch);

    echo $return;

?>

the only obvious problem I know of it that the server being queried uses GETinstead of POST. Are there GETalternatives to this method?

唯一明显的问题,我知道它的服务器所查询的用途GET,而不是POSTGET这种方法有替代方法吗?

回答by AlienWebguy

curl_setopt($ch, CURLOPT_POST, 0);

Curl uses GET by default. You were setting it to POST. You can override it if you ever need to with curl_setopt($ch, CURLOPT_HTTPGET, 1);

Curl 默认使用 GET。您将其设置为 POST。如果您需要,您可以覆盖它curl_setopt($ch, CURLOPT_HTTPGET, 1);

回答by Oyeme

Use file_get_contents() function
file_get_contents

使用 file_get_contents() 函数
file_get_contents

Or
curl_setopt($ch, CURLOPT_HTTPGET, 1);

或者
curl_setopt($ch, CURLOPT_HTTPGET, 1);

回答by Antara Das

use

curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "http://yourlink.com",
CURLOPT_USERAGENT => 'Codular Sample cURL Request'));

回答by miken32

All these years and nobody's given the right answer; the way to build a query string is to use http_build_query()with an array. This automatically escapes everything and returns a simple string.

这些年来,没有人给出正确的答案;构建查询字符串的方法是使用http_build_query()数组。这会自动转义所有内容并返回一个简单的字符串。

$xml = '<?xml version="1.0"?><Eligibility xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/var/lib/tomcat5/webapps/eligibility/Eligibilitywsdl.xsd"><PropertyRequest StreetAddress1="'.$street.'" StreetAddress2="" StreetAddress3="" City="'.$city.'" State="'.$state.'" County="" Zip="'.$zip.'" Program="RBS"></PropertyRequest></Eligibility>';
$data = [
    "eligibilityType" => "Property",
    "requestString" => $xml
];
$query = http_build_query($data);
$url .= "?$query";

回答by edwardmp

You are missing a question mark in the URL. Should be like:

您在 URL 中缺少一个问号。应该是这样的:

$query = '?eligibilityType=Property&...';

Also, that XML in your URL needs encoding, e.g. use the urlencode()function in PHP.

此外,您的 URL 中的 XML 需要编码,例如在 PHP 中使用urlencode()函数。