Save cURL Display Output String in Variable PHP
You need to set CURLOPT_RETURNTRANSFER
option to true.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
How to store curl response in a variable in PHP?
As described in the documentation ...
Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
So your code has to look like this
$result = curl_exec($ch);
// as you mentioned the response is a json string (screenshot)
$decoded_result = json_decode($result);
// output it on the screen
echo "<pre>";
var_dump($result);
echo "</pre>";
// insert into database (in case $result is an array)
$sql = "INSERT INTO table (column1, column2) VALUES (:result1, :result2)";
$pdo = new \PDO(...);
$stmt = $pdo->prepare($sql);
$stmt->execute([
':result1' => $result['bla'],
':result2' => $result['blubb'],
]);
That should be all you have to do.
Saving a curl response into a php variable
You can do like this
<?php
//URL of targeted site
$url = "http://www.yahoo.com/";
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
$output = curl_exec($ch);
//echo $output;
// close curl resource, and free up system resources
curl_close($ch);
?>
The $output
variable contains the response.
capturing curl stdout into variable in PHP
This fixed it by redirecting stdout output: 2>&1
So this works:
$result = shell_exec('curl -v http://domain.com/images/dir/dir/dir/file.jpg --user username:password -o /usr/www/htdocs/images/dir/dir/dir/file.jpg 2>&1');
It would be great to be able to use the PHP curl wrappers but not if it means opening a stub file on the source machine and then writing to it. The curl command line version just moves over the file, nice and neat. If anyone knows how to do what I'm trying to do using PHP wrappers, I'd love to see how you do it.
Get particular variable from curl response string
preg_match is your friend:
<?
$result = "Accepted=AVSAUTH:TEST:::829649376:N::U
ENTRYMETHOD=KEYED
historyid=829649376
MERCHANTORDERNUMBER=14700000186
orderid=646526156
PAYTYPE=MasterCard
recurid=0
refcode=829649376-TEST
result=1
Status=Accepted
transid=0";
preg_match_all("/^([^=]+)=(.*)$/m", $result, $regs, PREG_SET_ORDER);
$data = [];
foreach($regs as $reg) {
$data[$reg[1]] = $reg[2];
}
print_r($data);
?>
$data will be an hash Array:
Array
(
[Accepted] => AVSAUTH:TEST:::829649376:N::U
[ENTRYMETHOD] => KEYED
[historyid] => 829649376
[MERCHANTORDERNUMBER] => 14700000186
[orderid] => 646526156
[PAYTYPE] => MasterCard
[recurid] => 0
[refcode] => 829649376-TEST
[result] => 1
[Status] => Accepted
[transid] => 0
)
PHP, cURL, strpos - Compare substring from text file line by line with string from variable
Try $linex = rtrim($linex, "\r\n");
prior to the comparison to handle extra line-ending characters which are likely in your file.
Return php variable from CURL post
The problem is with curl request, please add below code for value in $result.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
after that :
$data = json_decode($result);
$id = $data->company->id;
echo $id;
Let me know in case any problem .
cURL code in PHP dumps output to the page
Use this option to curl_setopt()
:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
This will make curl_exec
return the data instead of outputting it.
To see if it was successful you can then check $result
and also curl_error()
.
Saving a curl response into database
try this
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$var = curl_exec($ch)
Hope this works!
to get the id
$var = json_decode($var);
$id = $var[0]->id;
Related Topics
Shell_Exec() Timeout Management & Exec()
Restrict Content Access to Logged in Users with PHP
Soapclient: How to Pass Multiple Elements with Same Name
Is Auto-Initialization of Multi-Dimensional Hash Array Possible in Ruby, as It Is in PHP
Call to Undefined Function Oci_Connect, PHP_Oci8_12C.Dll, Windows 8.1, PHP5.6.6
Set Woocommerce Cart Expiration
Xpath in Simplexml for Default Namespaces Without Needing Prefixes
In Simplexml, How to Add an Existing Simplexmlelement as a Child Element
How to Fetch Account Data from a Provider with Lightopenid
Http Auth via PHP - PHP_Auth_User Not Set
Replace Multiple New Lines with a Single Newline
Woocommerce: Display Also Product Variation Description on Cart Items
How to Store Variable Values Over Multiple Page Loads
How to Create a Nested Array Out of an Array in PHP