I want to use curl and post method then I want to get response header (not response source) like set-cookie etc
How can I do this?
I created the below code but it only shows response source:
$ch = curl_init();
curl_setopt($ch,
CURLOPT_URL,"https://www.example.com");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, 1);
$server_output = curl_exec ($ch);
echo ($server_output);
I tried getinfo($ch);
Related
First I send a POST request with a login and a password (admin authorization) and get some fields.
Next, I need to send GET request to API (check that client email exists), but I receive CURL error: The requested URL returned error: 401
In Postman GET request is working, what I am doing wrong?
This is my GET Request:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, "www.myurl.com/clients?email=a#a.a");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
You have to pass query string in case GET.
try below updated code.
$qry_str = "?email=a#a.a"
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_URL, 'www.myurl.com/clients.php',$qry_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
$result = curl_exec($ch);
I'm sending content from a PHP Curl script to an API.
I'm using this is to do a POST do my script while passing json headers
$query = new stdClass;
$query->test = 'test';
$query = json_encode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$res = curl_exec($ch);
curl_close($ch);
But when I trace what the content type of the request in on the API side, I get
var_dump($_SERVER['CONTENT_TYPE']);
//application/x-www-form-urlencoded
Shouldn't I get this instead?
application/json
You should use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER
CURLOPT_HEADER can be true/false and define whether include header to response or not
FYI:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
These lines are redundant as you are not using https
I want to pull some html pice of code with some data from an external website with cURL, but a recived a null response. If I type the url in browser i get the data that i want but i need to do that from my app via cURL or file_get_contents();
This is my code :
$_awb = '888000074599';
$url = 'http://89.45.196.45';
$post_fields = ':8080/?id=8IM*8J*9K&NT='.$_awb;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
curl_close($ch);
You should not be using CURLOPT_POSTFIELDS. You are trying to do a GET request with a query string, but postfields is intended for making POST requests.
So setting CURLOPT_URL to the proper (absolute) URL should work.
$url = "http://89.45.196.45:8080/?id=8IM*8J*9K&NT="
$nt = "888000074599";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $nt);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
I need to fetch data from myallocator.com. I have his api address api.myallocator.com,
but when I send my data to them, it failed. I wrote some code for that which is shown below. Basically that pms property management system site.By that you can manage your property with his api.
$url = "http://api.myallocator.com/";
$xmlRequestString='<?xml version="1.0" encoding="UTF-8"?>
<GetProperties>
<Auth>
<UserId>"xxxxxxx"</UserId>
<UserPassword>"xxxxx"</UserPassword>
<VendorId>"xxxxxxx"</VendorId>
<VendorPassword>"xxxxxxxx"</VendorPassword>
</Auth>
</GetProperties>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequestString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
$data = curl_exec($ch);
echo $data;
curl_close($ch);
Here Is the example of php curl with post try this
<?php
$ch = curl_init();
// set your site url
curl_setopt($ch, CURLOPT_URL,"http://testmysite.com/");
curl_setopt($ch, CURLOPT_POST, 1);
// postvar1, postvar2 .. are the parameters to send with post
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
if ($server_output == "OK") { ... } else { ... }
?>
I am trying to send the post parameter to a url
http://judis.nic.in/supremecourt/DateQry.aspx
and read the response in in php...
How to do I send and read the response...!
Thanks in advance..
My code...as Below comments
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://judis.nic.in/supremecourt/DateQry.aspx");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "button=Submit");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlday1=01");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlday2=31");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlmonth1=01");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlmonth2=01");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlreport=A");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlyear1=2013");
curl_setopt($ch, CURLOPT_POSTFIELDS, "ddlyear2=2013");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response=curl_exec($ch);
echo $response;
curl_close($ch);
?>
By making use of cURL
Here's an example
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://judis.nic.in/supremecourt/DateQry.aspx");
curl_setopt($ch, CURLOPT_HEADER, 0);
// say if your URL has any POST params
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=John");
// grab URL and pass it to the browser
$response=curl_exec($ch);
//Display your response
echo $response;
// close cURL resource, and free up system resources
curl_close($ch);
?>
EDIT:
Concatenate the fields like this
//Since you are using multiple fields .. do like this
$postFields="button=Submit&ddlday=01&ddlday2=31&ddlmonth1=01&ddlmonth2=01&ddlreport=A&ddlyear1=2013&ddlyear2=2013";
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);