Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use curl GET instead of POST

Tags:

php

curl

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?

like image 554
Plummer Avatar asked Mar 18 '13 21:03

Plummer


People also ask

Does curl default to get or POST?

"By default you use curl without explicitly saying which request method to use. If you just pass in a HTTP URL like curl example.com it will use GET. If you use -d or -F curl will use POST, -I will cause a HEAD and -T will make it a PUT." Everything you need to know.

How do I request curl in terminal?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.

How get curl URL in PHP?

Use curl_getinfo($ch) , and the first element ( url ) would indicate the effective URL.

What is curl request?

'cURL' is a command-line tool that lets you transmit HTTP requests and receive responses from the command line or a shell script. It is available for Linux distributions, Mac OS X, and Windows. To use cURL to run your REST web API call, use the cURL command syntax to construct the command.


2 Answers

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

like image 82
AlienWebguy Avatar answered Nov 15 '22 21:11

AlienWebguy


Use file_get_contents() function
file_get_contents

Or
curl_setopt($ch, CURLOPT_HTTPGET, 1);

like image 33
Oyeme Avatar answered Nov 15 '22 22:11

Oyeme