17

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

<?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 GET instead of POST. Are there GET alternatives to this method?

Plummer
  • 6,522
  • 13
  • 47
  • 75
  • That's a pretty long query string to be sending via GET. you may run into browser length limits and find that it's been decapitated or truncated. – Marc B Mar 18 '13 at 21:22
  • If I run it in a browser, it works fine. Would the rules for cURL be any different? – Plummer Mar 19 '13 at 14:06
  • WHY would you downvote? I'm asking if there's an alt to `POSTFIELDS` for `GET` method. – Plummer Mar 19 '13 at 14:37

5 Answers5

28
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);

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • But what about the POSTFIELDS? Do I just omit that and pass the query fields along with the URL? – Plummer Mar 19 '13 at 14:01
  • 2
    Ya you'd fix this var to be `$url_final = $url.'?'.$url_query;` and then use that in your `curl_setopt($ch, CURLOPT_URL, $url_final);` – AlienWebguy Mar 19 '13 at 17:15
4

Use file_get_contents() function
file_get_contents

Or
curl_setopt($ch, CURLOPT_HTTPGET, 1);

Oyeme
  • 11,088
  • 4
  • 42
  • 65
1

use

curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "http://yourlink.com",
CURLOPT_USERAGENT => 'Codular Sample cURL Request'));
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Antara Das
  • 19
  • 2
1

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.

$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";
miken32
  • 42,008
  • 16
  • 111
  • 154
0

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

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

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

edwardmp
  • 6,339
  • 5
  • 50
  • 77