0

I'm new to curl in PHP... and I was just wondering how to transform this curl command into PHP:

curl  https://ancient-test.chargebee.com/api/v1/portal_sessions \
     -u test_rdsfgfgfddsffds: \
     -d customer[id]="EXAMPLE" \
     -d redirect_url="https://yourdomain.com/users/3490343"

Right now I've got:

$post_data['customer']['id']    = "EXAMPLE";
$post_data['redirect_url']      = "http://" . SITE_URL . "/myaccount/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://ancient-test.chargebee.com/api/v1/portal_sessions");
curl_setopt($ch,CURLOPT_USERPWD,"test_rdsfgfgfddsffds:");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$output = curl_exec($ch);
curl_close($ch);

But I get the error message:

{"errors":[{"message":"There were errors while submitting"},{"param":"customer[id]","message":"cannot be blank"}]}

Thanks for your help!

Jan

4
  • is $customer['id'] empty? Commented Mar 30, 2016 at 20:00
  • Are you using Doctrine in your project? Commented Mar 30, 2016 at 20:05
  • php's curl extension doesn't handle multi.dimensional arrays (afaik). You should use curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post_data)); or create the first-dimension array key by hand. However, your current code should generate an array-to-string conversion warning. Are you sure the code shown is the one you test with? Commented Mar 30, 2016 at 20:34
  • Thanks Rangad, that worked for me! The other two libraries require Composer, which I don't have on shared hosting. Commented Mar 31, 2016 at 20:48

2 Answers 2

-1

https://github.com/CircleOfNice/CiRestClientBundle

This one has a beautiful api.

$client->post($url, $payload, $options);
Sign up to request clarification or add additional context in comments.

Comments

-1

Using curl here's probably the answer Posting with PHP and Curl, deep array

$post_data['customer[id]'] = "EXAMPLE";

Guzzle is an awesome HTTP client library wrapping curl in PHP that will make your life way easier :)

With guzzle v6 your php code would look like this :

$client = new GuzzleHttp\Client();
$res = $client->request('POST', 'https://ancient-test.chargebee.com/api/v1/portal_sessions', [
    'auth' => ['test_rdsfgfgfddsffds', 'password'],
    'json' => [customer => [id => 'EXAMPLE']]
]);

1 Comment

Would love to use Guzzle, but it requires Composer... which I don't have in a shared hosting environment.

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.