Two cURL requests showing the same data when loaded via AJAX - php

I'm making two seperate cURL requests in the same php page. When I load the page by itself, it works as expected, with each request returning different, correct data.
However, when I load the page via AJAX, the second request shows the same data as the first. Why is this happening? Code follows below:
$auth = base64_encode( 'user:'.$api_key );
$data = array(
'apikey' => $api_key,
);
$json_data = json_encode($data);
$ch = curl_init();
$ch2 = curl_init();
$curlopt_url = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id'];
curl_setopt($ch, CURLOPT_URL, $curlopt_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
$results = json_decode($result, true); ?>
$curlopt_url_b = "https://us7.api.mailchimp.com/3.0/reports/".$_GET['id'].'/sent-to/?count=5000 ';
curl_setopt($ch2, CURLOPT_URL, $curlopt_url_b);
curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch2, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Basic '.$auth));
curl_setopt($ch2, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_TIMEOUT, 10);
curl_setopt($ch2, CURLOPT_POST, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_POSTFIELDS, $json_data);
$recipient_result = curl_exec($ch2);
$recipients = json_decode($recipient_result, true);
When loaded via AJAX, $recipients == $results, when they should return results from completely different end points. What gives?

The problem was with the AJAX request, not the cURL request. The AJAX request had added an additional paramter to the URL (in this case, 'ajax=true'), which was feeding through into $_GET variable, and hence requesting the wrong endpoint. It loaded correctly when loaded independently because the URL was not changed by the JS.

Related

PHP Curl wrong headers?

I'm sending content from a PHP Curl script to an API.
I'm using this is to do a POST do my script while passing json headers
$query = new stdClass;
$query->test = 'test';
$query = json_encode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$res = curl_exec($ch);
curl_close($ch);
But when I trace what the content type of the request in on the API side, I get
var_dump($_SERVER['CONTENT_TYPE']);
//application/x-www-form-urlencoded
Shouldn't I get this instead?
application/json
You should use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER
CURLOPT_HEADER can be true/false and define whether include header to response or not
FYI:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
These lines are redundant as you are not using https

Reusing cookie in curl not working

first time here, and I need help, please!
I have two functions that do different things. The first, only do a login in an external website, and its working fine. In the second fuction, I need to do a POST with the login saved from the first. I use the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE with the same path in both functions, but when I print what I'm sending in each function with var_dump(curl_getinfo($ch)), the cookie only shows in the first one, and not in the second, causing a 401 Unauthorized Server as a result of the second function.
<?php
$cookie_path = dirname(__FILE__).'/cookie.txt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_conf['login_url']);
curl_setopt($ch, CURLOPT_REFERER, $login_conf['referer_header']);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, TRUE );
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_path);
$html = curl_exec($ch);
?>
Thats basically the content of the login function, and this:
<?php
$cookie_path = dirname(__FILE__).'/cookie.txt';
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, $purge_conf['request_url']);
curl_setopt($ch2, CURLOPT_REFERER, $refheader_submit);
curl_setopt($ch2, CURLOPT_POST, 1 );
curl_setopt($ch2, CURLOPT_POSTFIELDS, $query_purge);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_HEADER, TRUE );
curl_setopt($ch2, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($query_purge))
);
curl_setopt($ch2, CURLINFO_HEADER_OUT, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie_path);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_path);
$html_purge = curl_exec($ch2);
?>
Is the other one, that I need to sent a json post to other page, for witch I need to keep the login and cookie.
I dont know if I am doing something wrong, or if I am missing something.

Get some data from external website via cURL

I want to pull some html pice of code with some data from an external website with cURL, but a recived a null response. If I type the url in browser i get the data that i want but i need to do that from my app via cURL or file_get_contents();
This is my code :
$_awb = '888000074599';
$url = 'http://89.45.196.45';
$post_fields = ':8080/?id=8IM*8J*9K&NT='.$_awb;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
curl_close($ch);
You should not be using CURLOPT_POSTFIELDS. You are trying to do a GET request with a query string, but postfields is intended for making POST requests.
So setting CURLOPT_URL to the proper (absolute) URL should work.
$url = "http://89.45.196.45:8080/?id=8IM*8J*9K&NT="
$nt = "888000074599";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $nt);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);

GET method retrieve tickets with zendesk API (php)

I'm not very good in php, but i don't understand why my code is not working it's like the function is not even launch because i receive nothing not even an error.
here is my code:
<?php
define("ZDAPIKEY", "MYTOKEN");
define("ZDUSER", "MYUSER");
$lauchnfunction = curlWrap();
/* Note: do not put a trailing slash at the end of v2 */
function curlWrap()
{
$ch = curl_init();
$url = "https://cubber.zendesk.com/api/v2/tickets/recent.json";
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return $decoded;
$data = curlWrap("/tickets/recent.json", null, "GET");
var_dump($data);
}
?>
of course i replace MYTOKEN and MYUSER with my real value

curl fetching content of redirected webpage

I try to login in a site which has a combined login.
The redirect to this site works fine and the redirect back as well ... but only if I stop the PHP programm with die(). Unfortunately it is not stored in $return, which I would need to proceed.
Any ideas, what I am doing wrong? Much appreciated!
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, TRUE);
curl_setopt($ch, CURLOPT_POSTREDIR, TRUE);
curl_setopt($ch, CURLOPT_URL, URI_LOGIN);
$content = curl_exec($ch);
// PREPARING THE LOGIN
$referer = URI_LOGIN . '?ReturnUrl=' . urlencode('/returnUrl/');
curl_setopt($ch, CURLOPT_URL, $referer);
curl_setopt($ch, CURLOPT_REFERER, $referer);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE);
$post = ...;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post) );
curl_setopt($ch, CURLOPT_USERAGENT, '...');
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
$content = curl_exec($ch);
// LOGIN
curl_setopt($ch, CURLOPT_URL, COMBINED_LOGIN);
$post = ...;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post) );
$return = curl_exec($ch);
// ...
?>
There was a problem with javascript. It stucked on an 'in-between'-page and I had to make another curl request to get to the right page.
Now it is working fine!

Categories