0

Does anyone no if it is possible to store echoed results from a Curl script.

Example of script been submitted to:

\\some code to generate images using imagick and the post variables

$array = array(1,2,3,4,5,6,7,8,9);
$result = json_encode($array);
echo $result;

Example of Curl:

$id = 1;
$champ = array("product" => "1","id"=> $id,"occasion"=> $o,"field1" => "1991","field2" => "test","field3" =>"test1","field4" => "test2","field5" =>"test3","field6" =>"test4","field7" =>"test5","field8" =>"test6","test7");
foreach($champ as $key => $data){
    $field_string .= $key."=".$data."&";
}
rtrim($field_string, "&");

$url = "http://www.test.com/website/script.php";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST, count($champ));
curl_setopt($ch,CURLOPT_POSTFIELDS, $field_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, FALSE);
$result = curl_exec($ch);
$array = json_decode($result);
curl_close($ch);
var_dump($array);

If i var_dump($result) i get a bool(true) so i know that the script has executed correctly and the output shows on screen however i don't seem to be able to store the information into a variable to process.

Thank you in advance

5
  • You are not actually posting a POST variable named "submit", are you? Commented Jul 12, 2013 at 15:13
  • no this is just an example i will update the code above bad example Commented Jul 12, 2013 at 15:13
  • Please note i can view the output which is the json_encoded array but i am unable to store the data Commented Jul 12, 2013 at 15:15
  • curl_exec() should never return true when CURLOPT_RETURNTRANSFER is turned on. Maybe the problem is there. Where can you view the output, in the 2nd PHP script or when you point your browser to the first one? Commented Jul 12, 2013 at 15:16
  • I can view the output in the curl file which it has got from the script creating the images. I have tried without the CURLOPT_RETURNTRANSFER but it made no difference and sorry i meant it returned true when was set to false Commented Jul 12, 2013 at 15:17

1 Answer 1

0

As curl_exec documentation says:

curl_exec() 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.

In your case you turn CURLOPT_RETURNTRANSFER off. Turn it on by

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

and check the $result again.

Note: building a request using foreach loop and rtrim() you may damage it if your data contains & character.

Just use http_build_query() with your $champ :

curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($champ));
Sign up to request clarification or add additional context in comments.

1 Comment

thank you i think it was a combination of set to false and the foreach loop instead of http_build_query. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.