-1

Trying to access an api with a php curl get request. For the get request to work I must first authenticate with a post request, essentially signing in to the platform. I get a correct response from the post request but I can't get the get request to work for me. Here is my code:

<?php

  $login_url = "https://publisher-api.company.com/1.0/Publisher/Login";
  $user_pswd = array(
    "username" => "username1",
    "password" => "password1"
  );

  $report_url = "https://publisher-api.company.com/1.0/Publisher(#####)/Channels/RevenueReport";

  function httpGet($url)
  {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // curl_setopt($ch, CURLOPT_HEADER, true);

    $output = curl_exec($ch);

    if($output === false)
    {
      echo "Error Number:".curl_errno($ch)."<br>";
      echo "Error String:".curl_error($ch);
    }
    echo $output;

  }

  function httpPost($url_post, $params, $url_get)
  {
    foreach($params as $key=>$value) { $params_string .=$key.'='.$value.'&'; }
    rtrim($params_string, '&');

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url_post);
    curl_setopt($ch, CURLOPT_POST, count($params));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);

    $result = curl_exec($ch);
    echo $result;

    httpGet($url_get);

    curl_close($ch);
  }

  httpPost($login_url, $user_pswd, $report_url);

?>

This is the output that is echoed out:

{"value":{"publisher":{"active":"1","publisher_id":"#####"}}}1{"error":{"code":"SYSTEM.SERVICE.NOT_AUTHORIZED","message":"You are not authorized to use this service. Authenticate first."}}
bpr
  • 483
  • 1
  • 11
  • 23

1 Answers1

1
  1. Set error_reporting(E_ALL) at the top of your page

  2. When you call httpGet($urlG);, $urlG is not available within the scope of the function named httpPost()

  3. #1 would have prevented #2

  4. My eyes bleed at the site of $uurl, pparams, etc... Please make those variable names more meaningful

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • I'm sorry you're so offended. I get not authorized error message now. When I login with the post request there is a cookie set that must be sent with the get request according to the documentation. Not sure how that factors into a curl request? – bpr Jan 05 '16 at 16:44
  • 1
    @bpr Not offended. Just want you to pick up better habits especially while you're still learning. – MonkeyZeus Jan 05 '16 at 16:46
  • got some better habits, please stop bleeding! – bpr Jan 05 '16 at 16:50
  • @bpr Very nice! As for the new cookie issue which you mention, I think this post should help http://stackoverflow.com/questions/13020404/keeping-session-alive-with-curl-and-php – MonkeyZeus Jan 05 '16 at 16:54
  • Thanks for the link! Still frustrated though, tried to implement the method shown but I still get an error response, now stating that the HTTP method is not allowed... – bpr Jan 05 '16 at 17:04
  • @bpr That sounds like an entirely new issue. I recommend getting in touch with the company which provides this API; they will be most knowledgeable in helping with your new issue. If my answer has helped to solved your original issue then please feel free to accept it. Thank you – MonkeyZeus Jan 05 '16 at 18:46