Trying to diagnose an issue using PHP to cURL to an Internet location on a RedHat Linux server.
cURL is installed and working, and:
<?php var_dump(curl_version()); ?>
shows all the correct information in the output. The issue is I can use PHP to cURL to localhost on the box itself, but not the Internet (see below).
Normally I'd suspect the firewall, but I can cURL from the command line to the Internet without a problem. The box can also update it's own software packages, etc.
What am I missing? My test is:
<?php
function http_head_curl($url,$timeout=30)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
if ($res === false) {
throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
}
return trim($res);
}
// Succeeds, displaying headers
echo(http_head_curl('localhost'));
// Fails:
echo(http_head_curl('www.google.com'));
?>
Are you having DNS resolution issues on the server? It will always be able to resolve localhost but may not be able to resolve www.google.com. Try this:
var_dump(dns_get_record('www.google.com'));
If DNS if not resolving, you should get:
array(0) {}
If DNS is working, you should get an array something like this:
array(6) {
[0]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(13) "74.125.201.99"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[1]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.105"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[2]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.104"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[3]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.106"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[4]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.103"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
[5]=>
array(5) {
["host"]=>
string(14) "www.google.com"
["type"]=>
string(1) "A"
["ip"]=>
string(14) "74.125.201.147"
["class"]=>
string(2) "IN"
["ttl"]=>
int(375)
}
}
Related
I have a php file that is designed to go through an API for registering users. The first step of this is getting an auth token from the API via a post request. When I run it via my Local (by Flywheel) wordpress site it returns the auth correctly, but when I run the code on a live wordpress server it doesn't work.
Here is the related code. I am quite certain that the agent, secret, and url that I am passing are correct.
function getAuth($agent, $secret, $url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $agent . ":" . $secret);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
$result = curl_exec($curl);
var_dump(curl_getinfo($curl) . '<br/>');
var_dump(curl_errno($curl) . '<br/>');
var_dump(curl_error($curl) . '<br/>');
curl_close($curl);
$array = json_decode($result, true);
return $array;
}
Perhaps most vexing to me is the error messages I receive from the var dumps. $array returns null.
string(10) "Array
" string(6) "0
" string(5) "
Any light you can shine on this would be very much appreciated!
Edit: Removed the typecast, and it now returns... Things?
array(26) { ["url"]=> string(38) "https://api.ce21.com/token/service/396" ["content_type"]=> string(9) "text/html" ["http_code"]=> int(502) ["header_size"]=> int(140) ["request_size"]=> int(258) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.47892) ["namelookup_time"]=> float(0.315523) ["connect_time"]=> float(0.351211) ["pretransfer_time"]=> float(0.441982) ["size_upload"]=> float(0) ["size_download"]=> float(1477) ["speed_download"]=> float(3089) ["speed_upload"]=> float(0) ["download_content_length"]=> float(1477) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0.478869) ["redirect_time"]=> float(0) ["redirect_url"]=> string(0) "" ["primary_ip"]=> string(13) "23.96.209.155" ["certinfo"]=> array(0) { } ["primary_port"]=> int(443) ["local_ip"]=> string(15) "162.241.252.215" ["local_port"]=> int(16238) } int(0) string(0) ""
Edit two: This is the doc for the API I'm working with https://api.ce21.com/interact/index#!/Tokens/Tokens_Post
I am trying to get all Order Products of a given product_id within a collection of Orders using the following BigCommerce API call in PHP cURL:
Get an Order Product
$pid = $_POST['product_id'];
$filter = array("status_id"=>11);
$orders = Bigcommerce::getOrders($filter);
foreach($orders as $order) {
$oid = $order->id;
$URL = $store_url . "/api/v2/orders/$oid/products/$pid.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); //Get status code
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response);
echo "OID: " . $oid . " PID: " . $pid . " var_dump: ";
print_r(var_dump($result) . "<br/>");
}
Output:
OID: 113948 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113949 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113954 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113957 PID: 4860 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
At first, I thought maybe the Orders did not contain the product_id I was looking for so I added this code to the top of the loop:
//Set $pid equal to the product_id of the first Order Product in the Order.
$pid = $order->products[0]->product_id;
New output:
OID: 113948 PID: 3703 var_dump: array(1) { [0]=> object(stdClass)#3 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113949 PID: 3627 var_dump: array(1) { [0]=> object(stdClass)#220 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113954 PID: 3816 var_dump: array(1) { [0]=> object(stdClass)#225 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
OID: 113957 PID: 3646 var_dump: array(1) { [0]=> object(stdClass)#222 (2) { ["status"]=> int(404) ["message"]=> string(37) "The requested resource was not found." } }
Is what I'm trying to accomplish possible with the BigCommerce API, or do I have to iterate through all Order Products within the collection of Orders?
I have an array $batchRequest that looks like this:
array(5) {
[0]=>
array(0) {
}
[1]=>
object(Facebook\FacebookRequest)#18 (9) {
["app":protected]=>
object(Facebook\FacebookApp)#9 (2) {
["id":protected]=>
string(16) "xxxxxxx"
["secret":protected]=>
string(32) "xxxxxxxx"
}
["accessToken":protected]=>
string(49) "xxxxx|xxxxxxx"
["method":protected]=>
string(3) "GET"
["endpoint":protected]=>
string(75) "/10209064245580796?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
["headers":protected]=>
array(0) {
}
["params":protected]=>
array(0) {
}
["files":protected]=>
array(0) {
}
["eTag":protected]=>
NULL
["graphVersion":protected]=>
string(4) "v2.5"
}
[2]=>
object(Facebook\FacebookRequest)#17 (9) {
["app":protected]=>
object(Facebook\FacebookApp)#9 (2) {
["id":protected]=>
string(16) "xxxxx"
["secret":protected]=>
string(32) "xxxxxxx"
}
["accessToken":protected]=>
string(49) "xxxx|xxxxxxxx"
["method":protected]=>
string(3) "GET"
["endpoint":protected]=>
string(75) "/10208823390691752?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
["headers":protected]=>
array(0) {
}
["params":protected]=>
array(0) {
}
["files":protected]=>
array(0) {
}
["eTag":protected]=>
NULL
["graphVersion":protected]=>
string(4) "v2.5"
}
[3]=>
object(Facebook\FacebookRequest)#19 (9) {
["app":protected]=>
object(Facebook\FacebookApp)#9 (2) {
["id":protected]=>
string(16) "xxxxx"
["secret":protected]=>
string(32) "xxxxxxx"
}
["accessToken":protected]=>
string(49) "xxxxx|xxxxxxx"
["method":protected]=>
string(3) "GET"
["endpoint":protected]=>
string(74) "/1294280923934896?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
["headers":protected]=>
array(0) {
}
["params":protected]=>
array(0) {
}
["files":protected]=>
array(0) {
}
["eTag":protected]=>
NULL
["graphVersion":protected]=>
string(4) "v2.5"
}
[4]=>
object(Facebook\FacebookRequest)#20 (9) {
["app":protected]=>
object(Facebook\FacebookApp)#9 (2) {
["id":protected]=>
string(16) "xxxxx"
["secret":protected]=>
string(32) "xxxxxxxx"
}
["accessToken":protected]=>
string(49) "xxxxx|xxxxxxxxxx"
["method":protected]=>
string(3) "GET"
["endpoint":protected]=>
string(74) "/1274474365912572?fields=id%2Cname%2Cpicture%2Cgender%2Cfriends%2Cbirthday"
["headers":protected]=>
array(0) {
}
["params":protected]=>
array(0) {
}
["files":protected]=>
array(0) {
}
["eTag":protected]=>
NULL
["graphVersion":protected]=>
string(4) "v2.5"
}
}
So its an array whose elements are complex objects. I need to send them to another page called parallelImport.php. Here is what I've tried:
Using JSON
$data = array('batchArrayChild' => json_encode($batchRequest), 'app_id' => $appId, 'app_secret' => $appSecret);
$endpoint_url = 'https://some-domain.net/pages/parallelImport.php';
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
$result = $curl_response;
print_r($result);
As you can see I json_encodeed $batchRequest and send it via cURL, and here is what it outputs:
string(16) "[[],{},{},{},{}]"
Using http_build_query
$data = array('batchArrayChild' => http_build_query($batchRequest), 'app_id' => $appId, 'app_secret' => $appSecret);
$endpoint_url = 'https://some-domain.net/pages/parallelImport.php';
$curl = curl_init($endpoint_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
$result = $curl_response;
print_r($result);
And after I do var_dump($_POST['batchArrayChild']) on parallelImport.php it says:
string(0) ""
Do you know any other way in which I could send this array to execution script and to get some kind of response?
I don't like sending complex (internal) objects between systems, so I would have created a DTO (data transfer object) with public properties and used that object to send the data to avoid any coupling of objects between services. The DTO could even be of type stdClass if you want to make it simple.
If you want to make the services super dependent to each other including sharing state you could try to use serialize() on the data
I was working on using an api in php.
This is what I got for so far:
$keybotlink = file_get_contents('https://steamgaug.es/api/v2');
echo $keybotlink;
(Not much :D), Anyways, if I try to run this, The page is empty.
If I try to do
$w = stream_get_wrappers();
echo 'openssl: ', extension_loaded ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_dump($w);
This is the output:
openssl: yes
http wrapper: yes
https wrapper: yes
wrappers: array(22)
{ [0]=> string(13) "compress.zlib"
[1]=> string(4) "dict"
[2]=> string(3) "ftp"
[3]=> string(4) "ftps"
[4]=> string(6) "gopher"
[5]=> string(4) "http"
[6]=> string(5) "https"
[7]=> string(4) "imap"
[8]=> string(5) "imaps"
[9]=> string(4) "pop3"
[10]=> string(5) "pop3s"
[11]=> string(4) "rtsp"
[12]=> string(4) "smtp"
[13]=> string(5) "smtps"
[14]=> string(6) "telnet"
[15]=> string(4) "tftp"
[16]=> string(3) "php"
[17]=> string(4) "file"
[18]=> string(4) "glob"
[19]=> string(4) "data"
[20]=> string(3) "zip"
[21]=> string(4) "phar"
}
Thanks, Me
To allow https wrapper php_openssl extension mustbe enabled and allow_url_fopen must be set to on.
Add below lines in php.ini file if it does not exist:
extension=php_openssl.dll
allow_url_fopen = On
You can also use curl for this:
function getData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
if($result === false)
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo 'Operation completed without any errors';
}
curl_close($ch);
return $result;
}
print_r( json_decode( getData('https://steamgaug.es/api/v2') ) );
For download content from HTTPS sites use folowing code:
<?php
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://steamgaug.es/api/v2");
curl_setopt($ch, CURLOPT_HEADER, 0);
// if you want to connet to https page you can skip SSL verification by setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to 0
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0 );
// if you want to fetch result to a variable use CURLOPT_RETURNTRANSFER
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the $output variable
$output = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
echo $output;
CURL is very good and full of options tool to working with HTTP(S) requests.
I'm trying to put together a WordPress plugin and I want to grab a list of all categories (of other WordPress blogs) via XML-RPC. I have the following code and it looks like it works so far:
function get_categories($rpcurl,$username,$password){
$rpcurl2 = $rpcurl."/xmlrpc.php";
$params = array(0,$username,$password,true);
$request = xmlrpc_encode_request('metaWeblog.getCategories',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $rpcurl2);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$results = curl_exec($ch);
$res = xmlrpc_decode($results);
curl_close($ch);
return $res;
}
If I use $res I get the following string as the response: Array
If I use $results then I get:
categoryId17 parentId0 descriptionTest categoryDescription categoryNameTest
htmlUrlhttp://test.yoursite.com/?cat=17 rssUrlhttp://test.yoursite.com/?feed=rss2&cat=17
categoryId1 parentId0 descriptionUncategorized categoryDescription
categoryNameUncategorized htmlUrlhttp://test.yoursite.com/?cat=1
rssUrlhttp://test.yoursite.com/?feed=rss2&cat=1
I need to pull out the names after description so Uncategorized and Test in this case.
It's my first time coding in PHP. I got these results by echoing them to the page, so not sure if they get changed in that process or not...
By the way I modified the above code from one that posts to a WordPress blog remotely so maybe I haven't set some of the options correctly?
With var_dump($res) I get:
array(2) { [0]=> array(7) { ["categoryId"]=> string(2) "17" ["parentId"]=> string(1)
"0" ["description"]=> string(4) "Test" ["categoryDescription"]=> string(0) ""
["categoryName"]=> string(4) "Test" ["htmlUrl"]=> string(40)
"http://test.youreventwebsite.com/?cat=17" ["rssUrl"]=> string(54)
"http://test.youreventwebsite.com/?feed=rss2&cat=17" } [1]=> array(7) {
["categoryId"]=> string(1) "1" ["parentId"]=> string(1) "0" ["description"]=>
string(13) "Uncategorized" ["categoryDescription"]=> string(0) "" ["categoryName"]=>
string(13) "Uncategorized" ["htmlUrl"]=> string(39) "http://test.youreventwebsite.com/?cat=1"
["rssUrl"]=> string(53) "http://test.youreventwebsite.com/?feed=rss2&cat=1" } }
You need to iterate your array:
foreach($res as $item) {
echo $item['description'] . $item['categoryName'] . $item['htmlUrl']; //etc...
}