How to send HTML code with cURL? - php

am struggling with this for weeks now..
what is the correct way to send HTML-code with cURL ??
I tried base46_encode() and also json_encode() .. but on the reciever-side I always get a unuseable string.. how would i encode a html-code for sending?
$ch = curl_init($url);
$payload = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);

You can pass an array directly to cURL as documented on PHP.net, try turn the payload data into an array and send directly into cURL or via HTTP Build Query.
HTTP Build Query
Possible duplicate of:
PHP + curl, HTTP POST sample code?
and
Post HTML content via cURL

Related

PHP Post JSON With CURL

I'm trying to post JSON by using CURL, and I created a table to store the data and the response I get from the url that Im sending the data to.
However, the response keeps saying that my "Post data is empty", and I can't figure out why.
I tested it with websites like https://reqbin.com/ with the data that is stored in my table and it works fine, since I get a "successful" response.
Can someone help me figure out why my data keeps being emptied?
Below is my code
$url = "url that im trying to post Json to.";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($json_object));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
I've also tried using the http_build_query but it does not work.
Thanks in advance!
PHP recognizes application/x-www-form-urlencoded standard data type by default
// one
$post = $GLOBALS['HTTP_RAW_POST_DATA'];
// two
$post = file_get_contents("php://input");

Not able to send json data through api [duplicate]

This question already has answers here:
How to POST JSON Data With PHP cURL?
(7 answers)
Closed 5 years ago.
I am using an external api where i have been given the following details related to that api
API KEY (Mandatory Header) :- apikey : "xxxxx"
Stage Server Base Url :- https://example.com
To add Details API :- POST(/student/{student_id})
What I plan to do is:
I would be collecting data from users through codeigniter form and
then will forward the data to the above api and get a response from
the api and accordingly if its a success then would perform furthur
actions or else need to display error message
However for testing purpose i am using a static array and trying to send it to the api.
What actually is happening is I am getting a blank result and so not sure if it is working or not.
Following is my code
<?php
$apiKey = 'xxxx';
$url = 'https://www.example.com';
$data = array("name"=> "ABC","location"=> "loc","class"=> 'tenth',"area"=> "area");
$ch = curl_init( $url );
$payload = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
$headers= curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization: ' . $apiKey));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
echo "<pre>$result</pre>";
?>
PS:I am new to api and curl so a little explanation along with the solution would be appreciated
$ch = curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Authorization:Basic '.$apikey,
)
);
$result = json_decode(curl_exec($ch));
try to add whether its Basic or Bearer in authorization header. Hope it works for you.
you should use like below statement
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

How to get xml content that is generated by php

I have and xml url from a supplier which generates xml content dynamically with php like;
http://www.example.com/outputxml/index.php?xml_service_id=161
This url is valid for a static ip so I gave him my websites hosting ip. Is there a way to open that url in browser with data scraping? Because My internet connection has no static ip.
Thank you.
I have tried below code;
$url = 'http://www.example.com/outputxml/index.php?xml_service_id=161?';
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $xml );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
But it gave html format.
Save the content on your server with something like a wget and then serve it. Please notice that you are probably going to infringe the policy of the xml's author (I don't know the consequences or the policy itself, but you should be careful), so you might consider to at least add a .htacces authentication on your server's page, just to not make the xml public.

curl to php conversion, array + json

I have this curl code i need to convert it to php
curl -H "Content-Type: application/json" -d '{"command":"sendoffer",
"steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"],
"message": "Message"}' http://website:1337/
As you can see there is an array along with normal json.
"itemIDsToRequest":["4942877123","4892501549"]
I looked at many question like this and this, but couldn't understand how to implement it.
Im sorry im very new to curl command.
the array is part of a JSON string that is not interpreted but used as plain string data in CURL so it does not really matter what is in there; use the very same JSON string as from your commandline example, so:
$data = '{"command":"sendoffer", "steamID":"###############","token":"lkTR4VG2", "itemIDsToRequest":["4942877123","4892501549"], "message": "Message"}'
curl_setopt($ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
If your array is
$data = array("command"=>"sendoffer", "steamID"=>"###############","token"=>"lkTR4VG2", "itemIDsToRequest"=>array("4942877123","4892501549"),"message"=>"Message");
Then send it on its way as json format with curl like that :
$url = 'http://website:1337/';
$ch = curl_init( $url );
// Convert array to json string
$data_json = json_encode( $data );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_json );
// Indicate in the header that it is json data
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
// Send request
$result = curl_exec($ch);
curl_close($ch);

Send and Receive JSON using an https curl_exec call in PHP

I'm trying to make a curl call using PHP. My project has a flow like this
I redirect user to the server.
User uses the services and the server redirects him to a page on my application.
I would like to make a server to server call at this point to verify certain details.
Point to be noted - I post JSON data as well as receive JSON data as response.
I have tried this. I am able to send data but don't get any response.
Is this correct?
Please help!
$data = array("One" => "Onedata", "Two" => "Twodata");
$JsonData = json_encode($data);
$ch = curl_init('https://exampleurl');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $JsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($JsonData))
);
$result = curl_exec($ch);
As you mention in the comment:
The problem is with SSL verification of the host. It takes some effort (see http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/) to setup SSL correctly, so a workaround is to add the following two options:
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
Try to use the below code which works for me. and make sure the json_url is correct. When you run it on the browser you should get an output. Before try it using CURL, run in on the browser and see whether you get the output you want.
$json_url ='https://exampleurl';
// Initializing curl
$ch = curl_init( $json_url );
// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-type: application/json')
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$results = curl_exec($ch); // Getting jSON result string
$json_decoded = json_decode($results);

Categories