Jump to content

Curl return values


EchoFool

Recommended Posts

Hey,

 

I just managed to get my curl script working and when i execute it with :

 

echo curl_exec ($ch).'<br>'; 

 

I get this as a result, which i am trying to understand how i manipulate it to assign to variables..:

s=507183574&d=0&n=name.part5.rar1

 

Firstly that last character 1 i don't know why it keeps returning a 1 even when its incorrect it still does it which seems to be some thing im doing wrong... but it returns this string - how do i put them into variables :S ? It looks like GETs from how its worded :S

Link to comment
Share on other sites

Since it is a query string, parse_str should suit you well.

 

As for it returning one because you do not have the returntransfer set in the curl_setopt. So it is displaying the contents to the browser, there is no need to echo in this case. To fix it add this before you run the exec:

 

curl_setopt($ch, CURL_RETURNTRANSFER, true); // allows for transfer to be put into a string

 

Then change your echo to:

$return = curl_exec($ch);

 

Then add the parse string logic:

parse_str($return, $valueArray);
echo "<pre>", print_r($valueArray), "</pre>";

 

Should do you right.

Link to comment
Share on other sites

Post the code you tried it with.

 

EDIT:

An amendment to my first post:

 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // allows for transfer to be put into a string

 

Try that and it should work. Sorry about that.

Link to comment
Share on other sites

It was how I used print_r. Change it to this to remove the 1.

 

parse_str($return, $valueArray);
echo "<pre>", print_r($valueArray, true), "</pre>";

 

As the true will make it not return a true/false value. print_r for more information.

Link to comment
Share on other sites

It is an associative array, meaning it is not number indexed. You have to call it by 'd':

If($valueArray['d'] == 0){ echo 'hi'; }

 

If you want it to be both you can do this, however, the associative should be enough:

$i=0;
foreach ($valueArray as $val) {
    $valueArray[$i++] = $val;
}

 

Will give you a numbered index as well as the associative. So yea either or I guess.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.